curl to RESTapi specifying queryParam, pathParam and formData

huangapple go评论112阅读模式
英文:

curl to RESTapi specifying queryParam, pathParam and formData

问题

  1. 我正在编写一个包含APIRestControllerAPI应该接受QueryParamPathParamFormData作为输入我将展示给您代码
  2. @ApiResponses(value = { @ApiResponse(code = 403, message = "禁止访问/请求未经授权"),
  3. @ApiResponse(code = 412, message = "客户端错误"),
  4. @ApiResponse(code = 500, message = "内部服务器错误"),
  5. @ApiResponse(code = 200, message = "请求已接受") })
  6. @ApiOperation(httpMethod = "POST", value = "一些描述", tags = {
  7. "一些标签" }, notes = "一些注释")
  8. @RequestMapping(method = { RequestMethod.POST }, value = { "/foo/bar/{id}" }, produces = {
  9. "application/json" }, consumes = { "application/json" })
  10. public ResponseEntity moveDPO(@PathVariable(value = "id") String id,
  11. @RequestParam(value = "endpoint") @ApiParam(required = true, allowEmptyValue = false, example = "https://endpoint.com") String endpoint,
  12. @RequestPart(value = "json") String json)
  13. 正如您所见这里有三个参数
  14. - id -> 路径参数
  15. - endpoint -> 查询参数
  16. - json -> formData它是JSON
  17. 我尝试以以下方式使用curl调用此REST API
  18. curl -d 'json={"key1":"value1", "key2":"value2", ...}' https://mydomain/foo/bar/3541832?endpoint=https://someendpoint.com -H "Authorization: basic dXNlcjpwYXNzd29yZA==" -H "content-type: application/json"
  19. 然而当我尝试时我得到以下错误
  20. {"timestamp":"2020-08-20T14:45:36.940+0000","status":500,"error":"内部服务器错误","message":"无法解析多部分servlet请求;嵌套异常是javax.servlet.ServletException:org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException:请求不包含multipart/form-data或multipart/mixed流,内容类型头是application/json","path":"/foo/bar/3541832"}
  21. 我无法弄清楚如何使其工作为什么REST API 期望multipart/form-data这是因为@RequestPart注解吗
  22. 谢谢
英文:

I'm writing a RestController which contains an API which is supposed to take as input a QueryParam, a PathParam and a FormData. I'll show you the code:

  1. @ApiResponses(value = { @ApiResponse(code = 403, message = "Forbidden/Request Unauthorized"),
  2. @ApiResponse(code = 412, message = "Client Error"),
  3. @ApiResponse(code = 500, message = "Internal server error"),
  4. @ApiResponse(code = 200, message = "Request accepted") })
  5. @ApiOperation(httpMethod = "POST", value = "Some description", tags = {
  6. "some tag" }, notes = "some note")
  7. @RequestMapping(method = { RequestMethod.POST }, value = { "/foo/bar/{id}" }, produces = {
  8. "application/json" }, consumes = { "application/json" })
  9. public ResponseEntity moveDPO(@PathVariable(value = "id") String id,
  10. @RequestParam(value = "endpoint") @ApiParam(required = true, allowEmptyValue = false, example = "https://endpoint.com") String endpoint,
  11. @RequestPart(value = "json") String json)

As you can see, here I have three parameters:

  • id -> the path param
  • endpoint -> the query param
  • json -> the formData (it is a json)

I'm trying to call this RESTapi with curl in this way

  1. curl -d 'json={"key1":"value1", "key2":"value2", ...}' https://mydomain/foo/bar/3541832?endpoint=https://someendpoint.com -H "Authorization: basic dXNlcjpwYXNzd29yZA==" -H "content-type: application/json"

However, when I try, what I get is the following error:

  1. {"timestamp":"2020-08-20T14:45:36.940+0000","status":500,"error":"Internal Server Error","message":"Failed to parse multipart servlet request; nested exception is javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/json","path":"/foo/bar/3541832"}

I cannot figure out how to make it work. Why the RESTapi is expecting a multipart/form-data? It is because of the @RequestPart annotation?

Thank you.

答案1

得分: 0

我解决了。基本上有三个错误:

  • 首先,-d 选项是指请求体。在我的情况下,我想要将数据作为 formData 发送,所以 -F(或 --form)是正确的选项。
  • 第二,查询参数没有进行 URL 编码。
  • 第三,内容类型必须是 "content-type: multipart/form-data"

最终正确的 curl 命令如下:

curl -F 'json={"key1":"value1", "key2":"value2", ...}' https://mydomain/foo/bar/3541832?endpoint=https%3A%2F%2Fsomeendpoint.com -H "Authorization: basic dXNlcjpwYXNzd29yZA==" -H "content-type: multipart/form-data"

英文:

I solved it. Basically, there were three errors:

  • First one, the -d option refers to the body. In my case, I wanted to send data as formData, so -F (or --form) is the correct option
  • Second, the queryParam was not url-encoded
  • Third, the content-type must be "content-type: multipart/form-data"

The final correct curl is the following:

curl -F 'json={"key1":"value1", "key2":"value2", ...}' https://mydomain/foo/bar/3541832?endpoint=https%3A%2F%2Fsomeendpoint.com -H "Authorization: basic dXNlcjpwYXNzd29yZA==" -H "content-type: multipart/form-data"

huangapple
  • 本文由 发表于 2020年8月20日 23:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63508091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定