0

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 ?

RĂ¼diger Herrmann
  • 20,512
  • 11
  • 62
  • 79
jcheron
  • 193
  • 1
  • 9

1 Answers1

1

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")

Community
  • 1
  • 1
jcheron
  • 193
  • 1
  • 9