带有JSON正文和URL参数的HttpClient POST

时间:2017-06-07 01:42:35

标签: java json httpclient apache-nifi sfdc

我正在使用NiFi的InvokeHTTP处理器POST到SalesForce端点,特别是登录端点:

https://test.salesforce.com/services/oauth2/token

我将client_id,client_secret,用户名和密码字段附加到URL:

https://test.salesforce.com/services/oauth2/token?grant_type=password&client_id=<client_id>&client_secret=<client_secret>&username=<username>&password=<password + key>

另外还有一个通过InvokeHTTP处理器的JSON消息/有效负载,所以我配置

Content-Type: application/json

当我运行它时,这种方法很好。

[注意:那些不了解Apache NiFi但知道Java和/或SFDC中的HttpClient的人可以回答这个问题,我的观点是,在使用NiFi时,REST API端点适用于我,但是当我试图达到使用自定义Java代码的REST API]

现在因为我想将此机制转换为ExecuteScript处理器中的自定义代码,我尝试使用HttpClient库在Java中进行编码。但基于Baeldung中的这篇文章,似乎有多种方法可以做到这一点。我先尝试了第4项:

CloseableHttpClient client = HttpClients.createDefault();

String urlStr = "https://test.salesforce.com/services/oauth2/token?grant_type=password&client_id=<client_id>&client_secret=<client_secret>&username=<username>&password=<password + key>";
HttpPost httpPost = new HttpPost(urlStr);

String jsonPayload = "{\"qty\":100,\"name\":\"iPad 4\"}";
StringEntity entity = new StringEntity(jsonPayload);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

CloseableHttpResponse response = client.execute(httpPost);

System.out.println(response.getStatusLine().getStatusCode());

InputStream is = response.getEntity().getContent();
String responseStr = IOUtils.toString(is, "UTF-8");

System.out.println(responseStr);

client.close();

我得到了:

400
{"error":"invalid_grant","error_description":"authentication failure"}

我还在页面中尝试了#2模式,没有JSON有效负载,因为参数现在是我的实体:

CloseableHttpClient client = HttpClients.createDefault();

String urlStr = "https://test.salesforce.com/services/oauth2/token";

HttpPost httpPost = new HttpPost(urlStr);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
params.add(new BasicNameValuePair("username", USERNAME));
params.add(new BasicNameValuePair("password", PASSWORD));

httpPost.setEntity(new UrlEncodedFormEntity(params));

CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());

InputStream is = response.getEntity().getContent();
String responseStr = IOUtils.toString(is, "UTF-8");

System.out.println(responseStr);

client.close();

在这种情况下,我也得到相同的回复。我在Java代码中遗漏了什么吗?如何将paramsjsonPayload作为实体组合使用这两种模式?

1 个答案:

答案 0 :(得分:0)

以下是Bing search API URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/search"); builder.setParameter("q", line); builder.setParameter("count", "10"); builder.setParameter("offset", "0"); builder.setParameter("mkt", "en-us"); builder.setParameter("safesearch", "Moderate"); URI uri = builder.build(); HttpGet request = new HttpGet(uri); request.setHeader(api key); HttpClient httpclient = HttpClients.createDefault(); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity();

的体验
entity

稍后,您需要阅读{{1}}并按照您的意愿操作它。