[英]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);
一个自包含的、可重复的实体,它从字符串中获取其内容。
对于参数使用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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.