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

2

8

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 I have, but I am unable to make such a request in Java using Apache libraries.

In the code below, I am able to POST to the web service, and it correctly receives the contents of the variable raw_body. If I uncomment the first of the two commented lines, the web service receives the "fname" parameter, but it no longer receives the body of the POST.

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
...

HttpClient httpClient = new HttpClient();
String urlStr = "http://localhost:8080/MyRestWebService/save";
PostMethod method = new PostMethod(urlStr);
String raw_body = "This is a very long string, much too long to be just another parameter";
RequestEntity re = new StringRequestEntity(raw_body, "text/xml", "UTF-16");
//method.addParameter("fname", "test.txt");
//httpClient.getParams().setParameter("fname", "test.txt");
method.setRequestEntity(re);

How can I transmit both the parameter and the body?

Unstable answered 1/10, 2012 at 1:50 Comment(0)
M
4

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.

Mocambique answered 1/10, 2012 at 2:12 Comment(1)
Ah, this is perfect. setQueryString is exactly what I needed. Your comments about this not being very Restful and also that it should probably be a PUT request are well taken. Thanks for steering me in the right direction.Unstable
D
3

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.

Duumvir answered 1/10, 2012 at 1:53 Comment(2)
This shows me how to make a multi-part post if I had several large objects to send over (which is not applicable to my problem), and doesn't give me any information on how to include the parameter I need to pass.Unstable
that's not true. it is also applied to small parameters. adding query string to a POST request isn't recommended. some http handler software may not parse the query part of a POST request as it isn't expecting that. if it doesn't work on your server, that may be the cause. as long as it works on your server then you use whatever works best for you. but from design perspective, it isn't recommended.Duumvir

© 2022 - 2024 — McMap. All rights reserved.