Java Questions
Java Questions

Reputation: 7953

Java - sending HTTP parameters with apache.http.client.methods.HttpPost

i am trying to send application/json data using httpclient also i want to send some parameter with http post.

How to do this when using apache.http.client.methods.HttpPost.

Please can some one help me on this.

Best Regards

Upvotes: 0

Views: 968

Answers (1)

NilsH
NilsH

Reputation: 13821

I'm not sure if you can send post parameters and JSON at the same time, since the JSON string already will be the content of the request body. You could try sending the query parameters as part of the URL, and create a regular StringEntity for your JSON:

String jsonString = createMyJsonString();
HttpPost post = new HttpPost(urlWithQueryParams);
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity(jsonString,"UTF-8")); 

If you're posting to a REST service, it's common to include the parameters identifying the resource in the URL path. So if you have control over the end point, you could consider making the POST url independent of query/post parameters.

Upvotes: 2

Related Questions