Reputation: 165
I have a certain curl request as given below -
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'`
I tried making a java counterpart for the same , this is what I have come up till now -
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();
}
}
}
Please let me know how to fix this. Thank you in advance.
Upvotes: 1
Views: 290
Reputation: 5803
The way you are sending data in POST
request is not as per specified curl
command.
You should first make a JSON
string for your input. There are various libraries you can use for this purpose like Jackson
, JSON-java
, Gson
etc. or you can manually construct JSON
string (not recommended) and then you should send JSON
string as data in POST
request.
Below is one way to manually constructing JSON
string and then sending it as POST
data -
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();
}
}
}
Upvotes: 1