我有一个卷曲请求,如下所示 -
curl -POST -H 'access-key: <apikey>' -H "Content-type: application/json" -d '{
"item": "electricity",
"region": "india",
"unit": "kWh",
"quantity": 1.564}' 'https://www.carbonhub.xyz/v1/emissions'`
我尝试制作一个java对应物,这是我到现在为止所做的 -
package org.kodejava.example.httpclient;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class getEmissions {
public static void main(String[] args) {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://www.carbonhub.xyz/v1/emissions");
List<NameValuePair> data = new ArrayList<>(4);
data.add(new BasicNameValuePair("item", "electricity"));
data.add(new BasicNameValuePair("region", "india"));
data.add(new BasicNameValuePair("unit", "kWh"));
data.add(new BasicNameValuePair("quantity", 1.564));
try {
post.setEntity(new UrlEncodedFormEntity(data));
post.setHeader("Content-Type","application/json");
// use your api key
post.setHeader("access-key","<apikey>");
HttpResponse response = client.execute(post);
// Print out the response message
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
请让我知道如何解决这个问题。提前谢谢。
答案 0 :(得分:1)
您在POST
请求中发送数据的方式不符合指定的curl
命令。
您应首先为输入创建一个JSON
字符串。您可以使用各种库来实现此目的,例如Jackson
,JSON-java
,Gson
等,或者您可以手动构建JSON
字符串(不推荐)然后您应该发送JSON
字符串作为POST
请求中的数据。
以下是手动构建JSON
字符串然后将其作为POST
数据发送的一种方法 -
package org.kodejava.example.httpclient;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class getEmissions {
public static void main(String[] args) {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://www.carbonhub.xyz/v1/emissions");
StringBuilder requestData = new StringBuilder("'{");
requestData.append("\"item\"").append(':').append("\"electricity\"").append(',');
requestData.append("\"region\"").append(':').append("\"india\"").append(',');
requestData.append("\"unit\"").append(':').append("\"kWh\"").append(',');
requestData.append("\"quantity\"").append(':').append("1.564");
requestData.append("}'");
StringEntity requestDataEntity = new StringEntity(requestData.toString(),ContentType.APPLICATION_JSON);
try {
post.setEntity(requestData);
// use your api key
post.setHeader("access-key","<apikey>");
HttpResponse response = client.execute(post);
// Print out the response message
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}