curl到restapi指定queryparam、pathparam和formdata

t1rydlwq  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(358)

我正在编写一个restcontroller,它包含一个api,该api应该将queryparam、pathparam和formdata作为输入。我给你看代码:

@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)

如您所见,这里有三个参数:
id->路径参数
端点->查询参数
json->formdata(它是一个json)
我试着用这种方式把这个叫做restapi和curl

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

我想不出如何使它起作用。为什么restapi需要多部分/表单数据?是因为@requestpart注解吗?
谢谢您。

fcwjkofz

fcwjkofz1#

我解决了。基本上有三个错误:
第一个是 -d 选项是指主体。在我的例子中,我想以formdata的形式发送数据,所以 -F (或 --form )是正确的选择吗
其次,queryparam不是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"

相关问题