0

I want to send http POST via my java client.

Is there a way to send query params and a content in the body for a POST request?

here is my java http client:

@Override
public ResponseOrError sendPost(String url, String bodyContent) {
    url = urlUtils.getHttpUrl(url);

    ResponseOrError responseOrError = new ResponseOrError();
    final RetryListenerWithBooleanFlags listener = new RetryListenerWithBooleanFlags();
    try {

        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        Callable<ResponseOrError> callable = getCallable(httpPost);
        retryer = getRetryer(listener);
        responseOrError = retryer.call(callable);
        fillResponseOrError(responseOrError, listener);

    } catch (Exception e) {
        responseOrError.error = new Error();
        String errorMsg = getStatusCode(responseOrError, listener);
        responseOrError.error.errorMsg = e.getMessage() + errorMsg;
    }
    return responseOrError;
}
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

3 Answers3

0

Be sure to check Java API.

This should work.

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("key", "value"));
post.setEntity(new UrlEncodedFormEntity(params));
Max Luan
  • 305
  • 3
  • 9
0

Is there a reason to use HttpPost from org.apache.http.client?

Unfortunately I'm unfamiliar with that library / class, but if there is no specific reason to use this library an option would be to simply use HttpURLConnection

Example (not having a compiler at hand so it might have some mistakes in it):

URL url = new URL("http://..");
HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
httpCon.setRequestMethod("POST"); //it's a post request
httpCon.setDoInput(true); //read response
httpCon.setDoOutput(true); //send Post body
... = httpCon.getOutputStream(); //Here you go, do whatever you want with this stream
Code Monkey
  • 78
  • 1
  • 8
  • why should I prefer `HttpURLConnection` over `org.apache.http.client` ? – Elad Benda2 Apr 14 '16 at 10:54
  • @user1065869 maybe taste / requirements / maintenance. `org.apache.http.client` seems to be a third party library whereas `HttpURLConnection` should be directly available. adding a third party library means you should have a good plan to monitor it for updates (security releases) and apply those patches (besides it generally increases the attack surface). In the case of `HttpURLConnection`you'll simply have to run the newest jre (which you should do anyway). So my personal taste is mostly to stay away from third party libraries. – Code Monkey Apr 14 '16 at 11:13
  • But it depends on what the library does, how good it is maintained, and how much of it's functionality i need respectively how complex the task is. For a simple POST request i personally would stay with `HttpURLConnection`. But if the library would offer a lot of things i really need, I'd likely use it if actively maintained and the ppl behind it know what they do (from a security perspective) – Code Monkey Apr 14 '16 at 11:16
0

Just append the parameters to the URL, as easy as:

url = url + "?param=value&otherparam=othervalue";

Ensure you use:

  • ?: To start the query string
  • =: To associate the parameter with its value
  • &: To separate the parameter/value pairs

You will need to encode the parameter values if they include blank spaces, for example.

To do it, use the URLEncoder class:

String encoded = URLEncoder.encode(value, StandardCharsets.UTF_8);

With this, some value with blank spaces will become some%20value%20with%20blank%20spaces.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359