简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM