+ Start a Discussion
yajivyajiv 

Unsuccessful POST with REST on Custom Object

Hi,
I am trying to do an update of a Custom Object with java and Apache HttpClient 4 and getting a 400 Bad request error. I tried both _HttpPatch and the method below.Thanks for any help.

 

TIA,

Vijay

 

I blanked out he pod below to xxxxx-

 

URI:https://xxxxxx-salesforce.com/services/data/v20.0/sobjects/DeviceIdentifyingInfo__c/a1AK00000004HFaMAM

 status return code is not OK:400:HTTP/1.1 400 Bad Request
 
 Code snippet below:
 
 String url = "https://" + instanceUrl
  + "/services/data/v20.0/sobjects/DeviceIdentifyingInfo__c/"
  + escapedId;
//     + escapedId + "?_HttpMethod=PATCH";
 HttpPost post = new HttpPost(url)
{
 @Override
 public String getMethod() {
  return "PATCH";
 }
};


post.addHeader("Authorization", "OAuth " + sessionId);
HttpParams params = post.getParams();
HttpConnectionParams.setSoTimeout(params, connectionTimeoutInt);

post.setParams(params);
org.json.JSONObject device = new org.json.JSONObject();

try {
 device.put("Id", escapedId);

 device.put("Token__c", escapedToken);
}


StringEntity se = new StringEntity(device.toString());

post.setEntity(se);

post.setHeader("Content-type", "application/json");

// httpclient.executeMethod(get);
HttpResponse httpResponse = httpclient.execute(post);

if (httpResponse.getStatusLine() != null
&& httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {


} else {
//error 400<====
}

Best Answer chosen by Admin (Salesforce Developers) 
dkadordkador

Please post the error message you're getting in the HTTP response body.  That will give you details about what went wrong.

 

One thing that jumps out at me is that you do not need to specify the ID of the record in the JSON body.  The ID of the record comes from the URL.

All Answers

dkadordkador

Please post the error message you're getting in the HTTP response body.  That will give you details about what went wrong.

 

One thing that jumps out at me is that you do not need to specify the ID of the record in the JSON body.  The ID of the record comes from the URL.

This was selected as the best answer
yajivyajiv

Thanks.Just removing the ID from the JSON body resolved it.