How can I POST using Java and include parameters and a raw request body?

前端 未结 2 1044
醉酒成梦
醉酒成梦 2021-01-18 01:45

I am communicating with a web service that expects a POST parameter and also expect Request body. I have confirmed that such a POST request can be done using a REST Console

相关标签:
2条回答
  • 2021-01-18 02:10

    You need to make a POST request using multipart-form. Here is the example:

    Apache HttpClient making multipart form post

    Alternatively, you can make a POST request with the content (parameters and files) encoded using application/x-www-form-urlencoded but it is not recommended when you want to make a POST request with large content, like files.

    0 讨论(0)
  • 2021-01-18 02:13

    You could use the setQueryString method to add the parameters to the URL that is being POSTed to. From a RESTful perspective I'd argue you should normally not be doing that, however, since a POST should represent a call to a resource and anything that would qualify for a query parameter should be included in the representation that is being transferred in the request body...or it should represent qualification of the resource itself in which case it should be part of the path that is posted to which could then be extracted by the controller using @PathVariable/@PathParam or something similar. So in your case you could also be looking for something like POST /MyRestWebService/files/test.txt or more fittingly a PUT if you're saving the resource and know the URI. The code on the server could pull the filename out from a URL pattern.

    0 讨论(0)
提交回复
热议问题