curl to RESTapi specifying queryParam, pathParam and formData

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

curl to RESTapi specifying queryParam, pathParam and formData

问题

我正在编写一个包含API的RestController该API应该接受QueryParamPathParam和FormData作为输入我将展示给您代码

    @ApiResponses(value = { @ApiResponse(code = 403, message = "禁止访问/请求未经授权"),
			@ApiResponse(code = 412, message = "客户端错误"),
			@ApiResponse(code = 500, message = "内部服务器错误"),
			@ApiResponse(code = 200, message = "请求已接受") })
	@ApiOperation(httpMethod = "POST", value = "一些描述", tags = {
			"一些标签" }, notes = "一些注释")
	@RequestMapping(method = { RequestMethod.POST }, value = { "/foo/bar/{id}" }, produces = {
			"application/json" }, consumes = { "application/json" })
	public ResponseEntity moveDPO(@PathVariable(value = "id") String id,
			@RequestParam(value = "endpoint") @ApiParam(required = true, allowEmptyValue = false, example = "https://endpoint.com") String endpoint,
			@RequestPart(value = "json") String json)

正如您所见这里有三个参数
- id -> 路径参数
- endpoint -> 查询参数
- json -> formData它是JSON

我尝试以以下方式使用curl调用此REST API

    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" 

然而当我尝试时我得到以下错误

    {"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"}

我无法弄清楚如何使其工作为什么REST API 期望multipart/form-data这是因为@RequestPart注解吗

谢谢
英文:

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:

@ApiResponses(value = { @ApiResponse(code = 403, message = "Forbidden/Request Unauthorized"),
@ApiResponse(code = 412, message = "Client Error"),
@ApiResponse(code = 500, message = "Internal server error"),
@ApiResponse(code = 200, message = "Request accepted") })
@ApiOperation(httpMethod = "POST", value = "Some description", tags = {
"some tag" }, notes = "some note")
@RequestMapping(method = { RequestMethod.POST }, value = { "/foo/bar/{id}" }, produces = {
"application/json" }, consumes = { "application/json" })
public ResponseEntity moveDPO(@PathVariable(value = "id") String id,
@RequestParam(value = "endpoint") @ApiParam(required = true, allowEmptyValue = false, example = "https://endpoint.com") String endpoint,
@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

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:

{"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:

确定