Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make httpPost call with json encoded body?

i am using following code to make an httpPost call but it is returning me 400 bad request when i try to give following parameters in "simple rest client" in chrome extension it works fine any one guide me what mistake am i doing here?

Simple rest Client I entered the following:

URL: http://jon2012.com/api/register Method: POST Headers: No headers, as they are not required Data: { "email": "[email protected]", "first_name":"Name" }enter image description here

Android Code:

HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        JSONObject json = new JSONObject();
        try{
            HttpPost post = new HttpPost(url);
            json.put("email", email);
            json.put("first_name", name);
            StringEntity se = new StringEntity( "JSON: " + json.toString());  
            se.setContentEncoding(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
*/
            int statusCode = response.getStatusLine().getStatusCode();

        }
        catch(Exception e){
            e.printStackTrace();
           // createDialog("Error", "Cannot Estabilish Connection");
        }

any help would be appriciated

like image 620
UMAR-MOBITSOLUTIONS Avatar asked Jun 30 '11 10:06

UMAR-MOBITSOLUTIONS


People also ask

How do you send JSON in the request body?

Set the Request Content-Type Header Parameter. Set the “content-type” request header to “application/json” to send the request content in JSON form. This parameter has to be set to send the request body in JSON format.

Can you include JSON in a GET request?

To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode).

How do I send a JSON string in a POST request in go?

Select POST request and enter your service POST operation URL. Click on Headers. In the key column enter Content-Type and in the Value column enter application/json .

How pass JSON data in Curl POST?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.


1 Answers

I was making a common mistake sequence of json object was wrong. For example, I was sending it like first_name, email, etc. where as the correct sequence was email, first_name.

My code:

boolean result = false;
HttpClient hc = new DefaultHttpClient();
String message;

HttpPost p = new HttpPost(url);
JSONObject object = new JSONObject();
try {
    object.put("updates", updates);
    object.put("mobile", mobile);
    object.put("last_name", lastname);
    object.put("first_name", firstname);
    object.put("email", email);

} catch (Exception ex) {

}

try {
    message = object.toString();

    p.setEntity(new StringEntity(message, "UTF8"));
    p.setHeader("Content-type", "application/json");
    HttpResponse resp = hc.execute(p);
    if (resp != null) {
        if (resp.getStatusLine().getStatusCode() == 204)
            result = true;
    }

    Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
} catch (Exception e) {
    e.printStackTrace();

}

return result;
like image 157
UMAR-MOBITSOLUTIONS Avatar answered Sep 18 '22 09:09

UMAR-MOBITSOLUTIONS