SpringBoot:使用参数在 POST 请求上附加 json
SpringBoot: Attach json on POST request with params
我需要使用参数发出 POST
请求并附加 JSON 内容。
截至目前:
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();
}
这里curl
赞请求:
curl 'http://localhost:8983/solr/arxius/update?_=1605619902909&commitWithin=1000&overwrite=true&wt=json'
-H 'Content-type: application/json'
--data-raw $'[{ "id": ... }]'
使用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.
参数使用 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());
我需要使用参数发出 POST
请求并附加 JSON 内容。
截至目前:
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();
}
这里curl
赞请求:
curl 'http://localhost:8983/solr/arxius/update?_=1605619902909&commitWithin=1000&overwrite=true&wt=json'
-H 'Content-type: application/json'
--data-raw $'[{ "id": ... }]'
使用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.
参数使用 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());