需要帮助将 curl 请求转换为 java 请求



>我有一个特定的卷曲请求,如下所示-

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();
}
}
}

请让我知道如何解决这个问题。提前谢谢你。

您在请求中发送数据的方式POST不符合指定的curl命令。

您应该首先为输入创建一个JSON字符串。您可以使用各种库来实现此目的,例如JacksonJSON-javaGson等,或者您可以手动构造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();
}
}
}

最新更新