Reputation: 23277
I need to make a POST
request with params and attaching a JSON content.
Up to now:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8983/solr/arxius/update");
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("commitWithin", "1000"));
params.add(new BasicNameValuePair("overwrite", "true"));
params.add(new BasicNameValuePair("wt", "json"));
String json = "...";
// here I need to attach json as body...
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = client.execute(httpPost);
client.close();
} catch (IOException e) {
e.printStackTrace();
}
Here curl
like request:
curl 'http://localhost:8983/solr/arxius/update?_=1605619902909&commitWithin=1000&overwrite=true&wt=json'
-H 'Content-type: application/json'
--data-raw $'[{ "id": ... }]'
Upvotes: 2
Views: 489
Reputation: 58892
Use StringEntity:
StringEntity se = new StringEntity(json, org.apache.commons.lang3.CharEncoding.UTF_8);
httpPost.setEntity(se);
A self contained, repeatable entity that obtains its content from a String.
For parameters use URIBuilder
:
URIBuilder uriBuilder = new URIBuilder("http://localhost:8983/solr/arxius/update");
uriBuilder.addParameter("commitWithin", "1000");
...
HttpHost httpPost = new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());
Upvotes: 1