Apache HttpClient JSON Post - json

I try to send directly a JSON string with HttpClient 4.4 in an application (SWT/JFace) :
public String postJSON(String urlToRead,Object o) throws ClientProtocolException, IOException {
String result="";
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost postRequest = new HttpPost(urlToRead);
postRequest.setHeader("content-type", "application/x-www-form-urlencoded");
//postRequest.setHeader("Content-type", "application/json");
//{"mail":"admin#localhost", "password":"xyz"}
String jsonString=gson.toJson(o);
StringEntity params =new StringEntity(jsonString);
params.setContentType("application/json");
params.setContentEncoding("UTF-8");
postRequest.setEntity(params);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = httpClient.execute(postRequest, responseHandler);
}finally {
httpClient.close();;
}
return result;
}
I try to get the response from the server (Apache/PHP) with $POST
the correct content of $POST should be :
array("mail"=>"admin#localhost","password"=>"xyz")
When I use content-type : application/x-www-form-urlencoded
$POST content is :
array( "{"mail":"admin#localhost","password":"xyz"}"=> )
When I use content-type : application/json
$POST is empty : array()
Is there a way to post the JSON string with HttpClient or should I use an ArrayList<NameValuePair> and add each member of my object in the entity ?

I put the "NameValuePair" solution (not in the comments, the answer is too long), but I thought StringEntity was able to understand the JSON see How to POST JSON request using Apache HttpClient? and there: HTTP POST using JSON in Java
public String postJSON(String urlToRead,Object o) throws ClientProtocolException, IOException {
String result="";
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost postRequest = new HttpPost(urlToRead);
postRequest.setHeader("content-type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//{"mail":"admin#localhost", "password":"xyz"}
JsonElement elm= gson.toJsonTree(o);
JsonObject jsonObj=elm.getAsJsonObject();
for(Map.Entry<String, JsonElement> entry:jsonObj.entrySet()){
nameValuePairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue().getAsString()));
}
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = httpClient.execute(postRequest, responseHandler);
}finally {
httpClient.close();;
}
return result;
}
This way, the content of $POST is correct : array("mail"=>"admin#localhost","password"=>"xyz")

Related

How can I get the json file from ResponseEntity?

I have my web service consuming another web service. in my web service I modify some fields of the json. then in postman it is consumed ok.
Now I need to convert the ResponseEntity to a .json file to save it to a folder on my computer, but the attempt has failed.
Any ideas?
my controller current:
#Controller
public class SiviccController {
private final Logger log = LoggerFactory.getLogger(SiviccController.class);
private String url = "http://localhost:9090/url_1";
#GetMapping("/url_2")
#ResponseBody
public Body sivicc() {
RestTemplate restTemplate = new RestTemplate(Factory.timeoutHttp());
try {
return restTemplate.getForObject(url, Body.class);
} catch (Exception e) {
Body body = new Body();
body.setIserror(true);
log.info(e.getMessage());
return body;
}
}
}
Matheus Cirillo works perfectly with the following code:
#Controller
public class SiviccController {
private final Logger log = LoggerFactory.getLogger(SiviccController.class);
private String url = "http://localhost:9090/url_1";
#GetMapping("/url_2")
#ResponseBody
public Body sivicc() {
RestTemplate restTemplate = new RestTemplate(Factory.timeoutHttp());
try {
String response = restTemplate.getForObject(url, String.class);
BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
writer.append(response);
writer.close();
return restTemplate.getForObject(url, Body.class);
} catch (Exception e) {
Body body = new Body();
body.setIserror(true);
log.info(e.getMessage());
return body;
}
}
}
However, the web service that you were consuming before now generates a token. That's why I change getForObject to exchange and I consume it well, but I apply the same logic to save the json in a folder on my computer, but it generates a failure in two lines:
#Controller
public class SiviccController {
private final Logger log = LoggerFactory.getLogger(SiviccController.class);
private String url = "http://localhost:9090/url_1";
#GetMapping("/url_2")
#ResponseBody
public Body sivicc() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.set("Authorization", "bearer 8P7oM_ZDhB3TYolVkB1MLtM734DrrUSMVEFuiy3u");
HttpEntity request = new HttpEntity(headers);
headers.add("User-Agent", "Spring's RestTemplate" );
ResponseEntity<String> response = restTemplate.exchange(
url,
HttpMethod.GET,
request,
//Body.class,
String.class,
1
);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
writer.append(response);//Error: The method append(CharSequence) in the type Writer is not applicable for the arguments (ResponseEntity<String>)
writer.close();
return response.getBody();//Error: Type mismatch: cannot convert from String to Body
} catch (Exception e) {
Body body = new Body();
body.setIserror(true);
log.info(e.getMessage());
return body;
}
}
}
Change the Response type from getForObject to String.class, then, use the BufferedWriter to write the file.
RestTemplate rest = new RestTemplate();
String response = rest.getForObject("http://example.com/", String.class);
BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
writer.append(response);
writer.close();
Edit
The RestTemplate#exchange returns a ResponseEntity<T>, not a String.
In your case, it will return a ResponseEntity<String>.
That's why you're unable to write the object. To do so, you need to get the body from the ResponseEntity object. Use the method ResponseEntity#getBody
Something like this:
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class, 1);
BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
// here you are getting the String with the response.getBody() method,
// so buffered writer can write the file
writer.append(response.getBody());
writer.close();
Resolved:
add to pom.xml:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
Use the Jackson API ObjectMapper class to convert Java Object to a JSON string
Use the BufferedWriter to write the file. #Matheus Cirillo
ObjectMapper mapper = new ObjectMapper();
try {
json = mapper.writeValueAsString(response.getBody());
System.out.println("ResultingJSONstring = " + json);
BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
writer.append(json);
writer.close();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
thanks for your help =)

How to send JSON data in request to rest web service

I have created a rest webservice which has a below code in one method:
#POST
#Path("/validUser")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public JSONObject validUserLogin(#QueryParam(value="userDetails") String userDetails){
JSONObject json = null;
try{
System.out.println("Service running from validUserLogin :"+userDetails);
json = new JSONObject(userDetails);
System.err.println("UserName : "+json.getString("userName")+" password : "+json.getString("password"));
json.put("httpStatus","OK");
return json;
}
catch(JSONException jsonException) {
return json;
}
}
I am using Apache API in the client code.And below client code is calling this service, by posting some user related data to this service:
public static String getUserAvailability(String userName){
JSONObject json=new JSONObject();
try{
HttpContext context = new BasicHttpContext();
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
URI uri=new URIBuilder(BASE_URI+PATH_VALID_USER).build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/json");
json.put("userName", userName);
StringEntity stringEntity = new StringEntity(json.toString());
request.setEntity(stringEntity);
HttpResponse response = client.execute(request,context);
System.err.println("content type : \n"+EntityUtils.toString(response.getEntity()));
}catch(Exception exception){
System.err.println("Client Exception: \n"+exception.getStackTrace());
}
return "OK";
}
The problem is, I am able to call the service, but the parameter I passed in the request to service results in null.
Am I posting the data in a wrong way in the request. Also I want to return some JSON data in the response, but I am not able to get this.
With the help of Zack , some how i was able to resolve the problem,
I used jackson-core jar and changed the service code as below.
#POST
#Path("/validUser")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public JSONObject validUserLogin(String userDetails){
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(userDetails, JsonNode.class);
System.out.println("Service running from validUserLogin :"+userDetails);
System.out.println(node.get("userName").getTextValue());
//node.("httpStatus","OK");
return Response.ok(true).build();
}

Passing json data to a WebApi with special characters results to null

I have a json string that is being passed to a webapi, now the problem is, when I try adding special characters, the recieving object becomes null.
Here's I do it.
string json = JsonConvert.SerializeObject(ojectParams);
WebClient client = new WebClient();
client.Headers.Add("content-type", "application/json; charset=utf-8");
client.Headers.Add("AppKey", WebUser.AppKey);
client.Headers.Add("AppSecret", WebUser.AppSecret);
client.Headers.Add("AccountId", WebUser.AccountId.ToString());
if (!string.IsNullOrEmpty(WebUser.StoreId))
{
client.Headers.Add("StoreId", WebUser.StoreId);
}
var returnedStringObject = client.UploadString(string.Format("{0}/{1}", ConfigurationManager.AppSettings["Api"], endpoint), method, json);
Here's the json string:
"{\"Firstname\":\"kyv®\",\"Lastname\":\"sab®\"}"
I have added this one on the header hoping that it will fix the issue. But no luck with that.
charset=utf-8
On the recieving endpoint, the obj becomes null. But when I removed the special characters, the value is being passed.
[HttpPost]
public responseObj Endpoint(requestObj request)
Any ideas? Thanks!
You need to set the Encoding of the WebClient
client.Encoding = Encoding.UTF8;
Please see the code below.
Note: I did not use JsonConvert.SerializeObject and used HttpClient instead of WebClient
public static HttpRequestMessage CreateRequest(string requestUrl, HttpMethod method, String obj)
{
var request = new HttpRequestMessage
{
RequestUri = new Uri(requestUrl),
Method = method,
Content = new StringContent(obj, Encoding.UTF8, "application/json")
};
return request;
}
public static void DoAPI()
{
var client = new HttpClient();
var obj = "{\"Firstname\":\"kyv®\",\"Lastname\":\"sab®\"}";
var httpRequest = CreateRequest("mywebapiURL", HttpMethod.Post, obj);
var response = client.SendAsync(httpRequest).Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}

Read JSON with Jersey 2.0 (JAX-RS 2.0)

I was using Jersey 1.16 to consume a JSON, but now I'm with difficulties to consume a JSON using Jersey 2.0 (that implements JAX-RS 2.0).
I have a JSON response like this:
{
"id": 105430,
"version": 0,
"cpf": "55443946447",
"email": "maria#teste.br",
"name": "Maria",
}
and the method that consumes it:
public static JSONObject get() {
String url = "http://127.0.0.1:8080/core/api/person";
URI uri = URI.create(url);
final Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(uri);
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
if (response.getStatus() == 200) {
return response.readEntity(JSONObject.class);
}
}
I also tried:
return webTarget.request(MediaType.APPLICATION_JSON).get(JSONObject.class);
But the jSONObject return is null. I don't understand my error because the response is OK!
This is how to use the Response type correctly:
private void getRequest() {
Client client = ClientBuilder.newClient();
String url = "http://localhost:8080/api/masterdataattributes";
WebTarget target = client.target(url);
Response res = target
.request(MediaType.APPLICATION_JSON)
.get();
int status = res.getStatus();
String json = res.readEntity(String.class);
System.out.println(String.format("Status: %d, JSON Payload: %s", status, json));
}
If you're just interested in the payload, you could also just issue a get(String.class). But usually you will also want to check the response status, so working with the Response is usually the way to go.
If you want a typed (generic) JSON response, you could also have readEntity return a Map, or a list of Map if the response is an array of objects as in this example:
List<Map<String, Object>> json = res.readEntity(new GenericType<List<Map<String, Object>>>() {});
String id = (String) json.get(0).get("id");
System.out.println(id);
I have found the solution. Maybe it is not the best of, but it works.
public static JsonObject get() {
String url = "http://127.0.0.1:8080/core/api/person";
URI uri = URI.create(url);
final Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(uri);
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
//Se Response.Status.OK;
if (response.getStatus() == 200) {
StringReader stringReader = new StringReader(webTarget.request(MediaType.APPLICATION_JSON).get(String.class));
try (JsonReader jsonReader = Json.createReader(stringReader)) {
return jsonReader.readObject();
}
}
return null;
}
I switched the class JSONObject (package import org.codehaus.jettison) by JsonObject (package javax.json) and I used the methods to manipulate the content as String.
S.
mmey answer is the correct and optimal one, instead of invoking the service twice it does it one time.

how to send a json string in a POST request to web server in Java? [duplicate]

I would like to make a simple HTTP POST using JSON in Java.
Let's say the URL is www.site.com
and it takes in the value {"name":"myname","age":"20"} labeled as 'details' for example.
How would I go about creating the syntax for the POST?
I also can't seem to find a POST method in the JSON Javadocs.
Here is what you need to do:
Get the Apache HttpClient, this would enable you to make the required request
Create an HttpPost request with it and add the header application/x-www-form-urlencoded
Create a StringEntity that you will pass JSON to it
Execute the call
The code roughly looks like (you will still need to debug it and make it work):
// #Deprecated HttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://yoururl");
StringEntity params = new StringEntity("details={\"name\":\"xyz\",\"age\":\"20\"} ");
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
} catch (Exception ex) {
} finally {
// #Deprecated httpClient.getConnectionManager().shutdown();
}
You can make use of Gson library to convert your java classes to JSON objects.
Create a pojo class for variables you want to send
as per above Example
{"name":"myname","age":"20"}
becomes
class pojo1
{
String name;
String age;
//generate setter and getters
}
once you set the variables in pojo1 class you can send that using the following code
String postUrl = "www.site.com";// put in your url
Gson gson = new Gson();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(postUrl);
StringEntity postingString = new StringEntity(gson.toJson(pojo1));//gson.tojson() converts your pojo to json
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
and these are the imports
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
and for GSON
import com.google.gson.Gson;
#momo's answer for Apache HttpClient, version 4.3.1 or later. I'm using JSON-Java to build my JSON object:
JSONObject json = new JSONObject();
json.put("someKey", "someValue");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://yoururl");
StringEntity params = new StringEntity(json.toString());
request.addHeader("content-type", "application/json");
request.setEntity(params);
httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.close();
}
It's probably easiest to use HttpURLConnection.
http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
You'll use JSONObject or whatever to construct your JSON, but not to handle the network; you need to serialize it and then pass it to an HttpURLConnection to POST.
protected void sendJson(final String play, final String prop) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the childThread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 1000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost("http://192.168.0.44:80");
json.put("play", play);
json.put("Properties", prop);
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if (response != null) {
InputStream in = response.getEntity().getContent(); //Get the data in the entity
}
} catch (Exception e) {
e.printStackTrace();
showMessage("Error", "Cannot Estabilish Connection");
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
Try this code:
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost("http://yoururl");
StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} ");
request.addHeader("content-type", "application/json");
request.addHeader("Accept","application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
// handle response here...
}catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown();
}
I found this question looking for solution about how to send post request from java client to Google Endpoints. Above answers, very likely correct, but not work in case of Google Endpoints.
Solution for Google Endpoints.
Request body must contains only JSON string, not name=value pair.
Content type header must be set to "application/json".
post("http://localhost:8888/_ah/api/langapi/v1/createLanguage",
"{\"language\":\"russian\", \"description\":\"dsfsdfsdfsdfsd\"}");
public static void post(String url, String json ) throws Exception{
String charset = "UTF-8";
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/json;charset=" + charset);
try (OutputStream output = connection.getOutputStream()) {
output.write(json.getBytes(charset));
}
InputStream response = connection.getInputStream();
}
It sure can be done using HttpClient as well.
You can use the following code with Apache HTTP:
String payload = "{\"name\": \"myname\", \"age\": \"20\"}";
post.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));
response = client.execute(request);
Additionally you can create a json object and put in fields into the object like this
HttpPost post = new HttpPost(URL);
JSONObject payload = new JSONObject();
payload.put("name", "myName");
payload.put("age", "20");
post.setEntity(new StringEntity(payload.toString(), ContentType.APPLICATION_JSON));
For Java 11 you can use the new HTTP client:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost/api"))
.header("Content-Type", "application/json")
.POST(ofInputStream(() -> getClass().getResourceAsStream(
"/some-data.json")))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
You can use publishers from InputStream, String, File. Converting JSON to a String or IS can be done with Jackson.
Java 11 standardization of HTTP client API that implements HTTP/2 and Web Socket, and can be found at java.net.HTTP.*:
String payload = "{\"name\": \"myname\", \"age\": \"20\"}";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create("www.site.com"))
.header("content-type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
Java 8 with apache httpClient 4
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("www.site.com");
String json = "details={\"name\":\"myname\",\"age\":\"20\"} ";
try {
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
// set your POST request headers to accept json contents
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
try {
// your closeablehttp response
CloseableHttpResponse response = client.execute(httpPost);
// print your status code from the response
System.out.println(response.getStatusLine().getStatusCode());
// take the response body as a json formatted string
String responseJSON = EntityUtils.toString(response.getEntity());
// convert/parse the json formatted string to a json object
JSONObject jobj = new JSONObject(responseJSON);
//print your response body that formatted into json
System.out.println(jobj);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
I recomend http-request built on apache http api.
HttpRequest<String> httpRequest = HttpRequestBuilder.createPost(yourUri, String.class)
.responseDeserializer(ResponseDeserializer.ignorableDeserializer()).build();
public void send(){
ResponseHandler<String> responseHandler = httpRequest.execute("details", yourJsonData);
int statusCode = responseHandler.getStatusCode();
String responseContent = responseHandler.orElse(null); // returns Content from response. If content isn't present returns null.
}
If you want send JSON as request body you can:
ResponseHandler<String> responseHandler = httpRequest.executeWithBody(yourJsonData);
I higly recomend read documentation before use.