在 CURL 命令中传递 json 结构或数组数据来测试我的 REST 服务
Pass json structure or array data in CURL command to test my REST service
我希望使用 CURL 命令在 dropwizard 上测试我的 REST 资源。我能够上传文件并获取内容和文件名。但是除了文件之外,我还想传递一些 ID 列表。
- 如何在下面的 CURL 命令中将长 ID 列表(Long 数据类型数组)传递给 REST 服务?
- 如果需要,我可以在下面的命令中额外传递一个 json 结构和我的文件内容吗?
CURL 命令
curl -F 'file=@/cygdrive/c/TestDocument1.txt' http://localhost:8199/test-app/api/upload-documents/1004/documents
上传文件的REST服务
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/reconciliation-details/{reconciliationDetailId}/documents")
public Response uploadDocument(@FormDataParam("file") File inventoryDocumentContent,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@FormDataParam("reconciliationIds") List<Long> reconciliationIds) throws Exception {
byte[] documentContent = FileUtils.readFileToByteArray(inventoryDocumentContent);
String documentName = fileDetail.getFileName();
reconDetailsService.uploadDocument(documentName, documentContent, reconciliationIds);
return ResponseHelper.createOkResponse();
}
您需要将内容类型设置为 application/json。但是 -d 发送 Content-Type application/x-www-form-urlencoded,Spring 端不接受。
查看 curl 手册页,我认为您可以使用 -H:
-H "Content-Type: application/json"
完整示例:
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login
(-H 是 --header 的缩写,-d 是 --data 的缩写。)
Json 文件
curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=@/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith@gmail.com\"};type=application/json" http://localhost:8080/api/v1/user/
有了 multipart,每个部分都可以有自己的 Content-Type header。默认情况下,如果未指定,则使用 text/plain
。一些客户端无法为个别部分设置Content-Type,as mentioned in this post(对此有解决方案),但cURL 具有此功能。您只需要附加 ;type=<content-type>
。例如
-F "reconciliationIds=[1, 2, 3, 4];type=application/json"
除了使用实际的 JSON 数组,您还可以使用包含 JSON
的文件
-F "reconciliationIds=@path-to-json;type=application/json"
Jersey 将看到这部分的 Content-Type 是 application/json 并使用它通常用于 JSON
的反序列化器
确保看到上面的链接 post - 尽管这可能有效,但您不想限制您的用户只能使用具有此功能的客户端. post 的底部为您提供了解决方案,尽管我没有使用 List<Long>
对其进行测试。我不确定使用 getEntityAs(Class)
会如何工作,因为您将无法传递通用 Long
类型。您始终可以将其包装在 POJO 中,而不仅仅是使用列表。
我希望使用 CURL 命令在 dropwizard 上测试我的 REST 资源。我能够上传文件并获取内容和文件名。但是除了文件之外,我还想传递一些 ID 列表。
- 如何在下面的 CURL 命令中将长 ID 列表(Long 数据类型数组)传递给 REST 服务?
- 如果需要,我可以在下面的命令中额外传递一个 json 结构和我的文件内容吗?
CURL 命令
curl -F 'file=@/cygdrive/c/TestDocument1.txt' http://localhost:8199/test-app/api/upload-documents/1004/documents
上传文件的REST服务
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/reconciliation-details/{reconciliationDetailId}/documents")
public Response uploadDocument(@FormDataParam("file") File inventoryDocumentContent,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@FormDataParam("reconciliationIds") List<Long> reconciliationIds) throws Exception {
byte[] documentContent = FileUtils.readFileToByteArray(inventoryDocumentContent);
String documentName = fileDetail.getFileName();
reconDetailsService.uploadDocument(documentName, documentContent, reconciliationIds);
return ResponseHelper.createOkResponse();
}
您需要将内容类型设置为 application/json。但是 -d 发送 Content-Type application/x-www-form-urlencoded,Spring 端不接受。
查看 curl 手册页,我认为您可以使用 -H:
-H "Content-Type: application/json"
完整示例:
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login
(-H 是 --header 的缩写,-d 是 --data 的缩写。)
Json 文件
curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=@/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith@gmail.com\"};type=application/json" http://localhost:8080/api/v1/user/
有了 multipart,每个部分都可以有自己的 Content-Type header。默认情况下,如果未指定,则使用 text/plain
。一些客户端无法为个别部分设置Content-Type,as mentioned in this post(对此有解决方案),但cURL 具有此功能。您只需要附加 ;type=<content-type>
。例如
-F "reconciliationIds=[1, 2, 3, 4];type=application/json"
除了使用实际的 JSON 数组,您还可以使用包含 JSON
的文件-F "reconciliationIds=@path-to-json;type=application/json"
Jersey 将看到这部分的 Content-Type 是 application/json 并使用它通常用于 JSON
的反序列化器确保看到上面的链接 post - 尽管这可能有效,但您不想限制您的用户只能使用具有此功能的客户端. post 的底部为您提供了解决方案,尽管我没有使用 List<Long>
对其进行测试。我不确定使用 getEntityAs(Class)
会如何工作,因为您将无法传递通用 Long
类型。您始终可以将其包装在 POJO 中,而不仅仅是使用列表。