1
我有一定的捲曲請求 -需要幫助轉換成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();
}
}
}
請讓我知道如何解決這個問題。先謝謝你。
'UrlEncodedFormEntity'是用於編碼內容類型'應用程序/ x-WWW的形式urlencoded'。您需要將'data'轉換爲JSON,最好使用JSON庫。 – Andreas