Search code examples
javahttp-postapache-httpcomponents

Query parameters with Apache http post


I'm trying to access an API which returns Json content with query params through Apache http post. The endpoint works fine on the browser but my post request is completely ignoring the query params. It works completely fine on the browser.

HttpPost post = new HttpPost(url);// http://hostname:port/type/endpoint1
post.addHeader("content-type","text/plain");
post.addHeader("Accept","text/plain");
post.setEntity(new StringEntity("?key=value"));
response = client.execute(post);

This returns the response only for url. And is completely ignoring the params.


Solution

  • Change your content-type to application/x-www-form-urlencoded, and remove the leading ? from the body.