You need to sign in to do that
Don't have an account?
Response Code 400 while working on "PATCH"
I am trying to update the values by passing the account id
but i am getting the ERROR has RESPONSE CODE 400.
CAN ANY ONE HELP ME WHAT WENT WRONG IN MY CODE.
package com.JSON.Response;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Collection;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONArray;
import org.json.JSONObject;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.impl.client.DefaultHttpClient;
public class EndPointQuery {
private static final Collection newname = null;
private static JSONObject oauthLogin(String loginUri, String clientId, String clientSecret, String username, String password) {
// HttpRequest req = new HttpRequest(); u
return null;
}
private static JSONArray posturlconnection(String loginUri, String clientId, String clientSecret, String username, String password) {
try {
String strUrl = loginUri + "/services/oauth2/token", strInstanceURL = "", strAccessToken = "";
String strURLParameter = "grant_type=password&client_id=" + clientId + "&client_secret=" + clientSecret + "&username=" +
URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");
URL wsURL = new URL(strUrl);
HttpURLConnection wsCon = (HttpURLConnection) wsURL.openConnection();
wsCon.setRequestMethod("POST");
wsCon.setDoInput(true);
wsCon.setDoOutput(true);
wsCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//wsCon.setRequestProperty("Content-Type", "application/json");
wsCon.setRequestProperty("Contect-Length", "" + Integer.toString(strURLParameter.getBytes().length));
wsCon.setRequestProperty("charset", "utf-8");
OutputStreamWriter writer = new OutputStreamWriter(wsCon.getOutputStream());
writer.write(strURLParameter);
writer.flush();
writer.close();
String line = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(wsCon.getInputStream()));
while ((line = reader.readLine()) != null)
{
JSONObject jsnObj = new JSONObject(line);
strInstanceURL = jsnObj.getString("instance_url");
strAccessToken = jsnObj.getString("access_token");
}
JSONArray jsnArray = null;
String accountId="0019000000SvJkt";
PostMethod patch = new PostMethod(strInstanceURL + "/services/data/v20.0/sobjiects/Account/" + accountId + "?_HttpMethod=PATCH");
HttpURLConnection wsConQry = (HttpURLConnection) wsURL.openConnection();
System.out.println("Open Connection@@@@@@@@@@@@@@@@@@@"+wsConQry);
wsConQry.setRequestMethod("POST");
wsConQry.setRequestProperty("Authorization", "OAuth " + strAccessToken);
System.out.println("ResponseCode"+wsConQry.getResponseCode());
if(wsConQry.getResponseCode() == HttpURLConnection.HTTP_OK)
{
jsnArray = new JSONArray(line);
JSONObject update1=new JSONObject();
update1.put("Name", newname);
System.out.println("Updatename");
HttpClient httpclient = new HttpClient();
httpclient.executeMethod(patch);
/*BufferedReader readerRes = new BufferedReader(new InputStreamReader(wsConQry.getInputStream()));
while ((line = readerRes.readLine()) != null) {
jsnArray = new JSONArray(line);
JSONObject update1=new JSONObject();
update1.put("Name", newname);
System.out.println("Updatename");
HttpClient httpclient = new HttpClient();
httpclient.executeMethod(patch);
}*/
System.out.println(jsnArray.toString());
return jsnArray;
}
/*System.out.println(jsnArray.toString());
return jsnArray;*/
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
return null;
}
public static String restTest(String name) {
posturlconnection("https://login.salesforce.com", "3MVG9Y6d_Btp4xp4O7MWPf5592ThVeMSzmSMxLE.c8Efg.._MSWi1TIkCuwgg0n6INTp8C",
"4455896129340", "username","Password");
return null;
}
}
REGARDS
sailer
400 The request couldn’t be understood, usually because the JSON or XML body contains an error.
http://www.salesforce.com/us/developer/docs/api_rest/Content/errorcodes.htm
DELETE, GET, PATCH, POST, PUT 400 An unhandled user exception occurred.
There are couple of examples and sample code Given to explain when we get this particular error in the below doc. (Search for 400)
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_rest_methods.htm
Regards,
Ashish