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 ?

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.