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" }
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
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.
To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode).
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 .
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With