its all perfect! why is this happening? the same endpoint and JSON works in workbench but not in my java code:
REST API :
@RestResource(urlMapping='/RestTest/*')
global with sharing class restTestShyam {
@HttpPost
global static String testRest(String t1, String t2) {
System.debug('TestVar1: '+T1);
System.debug('TestVar2: '+T2);
//System.debug('TestVar2: '+testVar3);
return 'Shyam your test is working';
}
}
My JavaCode to call it:
public void restPostExample4() {
System.out.println("\n_______________ OUTPUT _______________");
String uri = baseUri + "/services/apexrest/RestTest";
try {
// create the JSON object containing the new contact details.
JSONObject applicant = new JSONObject();
applicant.put("t1", "shyam");
applicant.put("t2", "raju");
System.out.println("JSON for retriving record from test:\n" + applicant.toString(1));
// Construct the objects needed for the request
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader(oauthHeader);
httpPost.addHeader(prettyPrintHeader);
// The message we are going to post
StringEntity body = new StringEntity(applicant.toString(1));
body.setContentType("application/json");
httpPost.setEntity(body);
// Make the request
HttpResponse response = httpClient.execute(httpPost);
// Process the results
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 201 || statusCode == 200) {
String response_string = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(response_string);
System.out.println("Retrive Successful! Status code returned is " + statusCode);
System.out.println("Response from RestTest: " + json);
System.out.println("httpPOST: " + httpPost);
// Store the retrieved data as follows.
// eg.....
// contactId = json.getString("id");
} else {
System.out.println("Retrive unsuccessful. Status code returned is " + statusCode);
System.out.println(getBody(response.getEntity().getContent()));
System.out.println("httpPOST: " + httpPost);
}
} catch (JSONException e) {
System.out.println("Issue creating JSON or processing results");
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (NullPointerException npe) {
npe.printStackTrace();
}
}
My Output:
_______________ OUTPUT _______________
JSON for retriving record on applicant from yesterday:
{
"t1": "shyam",
"t2": "raju"
}
Retrive unsuccessful. Status code returned is 400
[ {
"message" : "Unexpected parameter encountered during deserialization: t1 at [line:2, column:9]",
"errorCode" : "JSON_PARSER_ERROR"
} ]
httpPOST: POST https://XX.salesforce.com/services/apexrest/XX/v37.0/services/apexrest/RestTest HTTP/1.1
Program complete.
workbench output:
PLEASE let me know what the problem is thanks! is it in my java code? or my apex class? but how is the same json i make as output running on workbench but not through java, im using the access token in the header and my login is successful so please dont tell me thats why...thanks guys really appreciate it!
SFDC error log:
39.0 APEX_CODE,FINER;APEX_PROFILING,NONE;CALLOUT,NONE;DB,INFO;SYSTEM,DEBUG;VALIDATION,NONE;VISUALFORCE,FINER;WAVE,INFO;WORKFLOW,FINER
17:44:40.0 (439003)|USER_INFO|[EXTERNAL]|asajshahjad|--------|Central Standard Time|GMT-06:00
17:44:40.0 (464306)|EXECUTION_STARTED
17:44:40.0 (468050)|CODE_UNIT_STARTED|[EXTERNAL]|01p210000009mIA|OutREST.OutREST
17:44:40.0 (635706)|HEAP_ALLOCATE|[72]|Bytes:3
17:44:40.0 (678017)|HEAP_ALLOCATE|[77]|Bytes:152
17:44:40.0 (692694)|HEAP_ALLOCATE|[342]|Bytes:408
17:44:40.0 (710332)|HEAP_ALLOCATE|[355]|Bytes:408
17:44:40.0 (726966)|HEAP_ALLOCATE|[467]|Bytes:48
17:44:40.0 (751711)|HEAP_ALLOCATE|[139]|Bytes:6
17:44:40.0 (792234)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:252
17:44:40.0 (796299)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:25
17:44:40.0 (807616)|SYSTEM_METHOD_ENTRY|[1]|RestRequest.RestRequest()
17:44:40.0 (811516)|STATEMENT_EXECUTE|[1]
17:44:40.0 (820119)|SYSTEM_METHOD_EXIT|[1]|RestRequest
17:44:40.0 (826856)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (845941)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
17:44:40.0 (848114)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:24
17:44:40.0 (1180053)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1184520)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:13
17:44:40.0 (1185920)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1
17:44:40.0 (1252081)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1255059)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:11
17:44:40.0 (1256360)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:44
17:44:40.0 (1282295)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1284585)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10
17:44:40.0 (1285909)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10
17:44:40.0 (1307325)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1309623)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10
17:44:40.0 (1310864)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:40
17:44:40.0 (1331573)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1333825)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
17:44:40.0 (1335128)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:19
17:44:40.0 (1355759)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1357945)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:44:40.0 (1359225)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
17:44:40.0 (1379604)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1381767)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
17:44:40.0 (1383102)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:44:40.0 (1437991)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:156
17:44:40.0 (1441627)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10
17:44:40.0 (1448782)|SYSTEM_METHOD_ENTRY|[1]|RestResponse.RestResponse()
17:44:40.0 (1451342)|STATEMENT_EXECUTE|[1]
17:44:40.0 (1456238)|SYSTEM_METHOD_EXIT|[1]|RestResponse
17:44:40.0 (1460351)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:44:40.0 (1548789)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:112
17:44:40.0 (1552754)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
17:44:40.0 (1559294)|SYSTEM_METHOD_ENTRY|[2]|RestContext.RestContext()
17:44:40.0 (1561352)|STATEMENT_EXECUTE|[2]
17:44:40.0 (1565730)|SYSTEM_METHOD_EXIT|[2]|RestContext
17:44:40.0 (1570102)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
17:44:40.0 (1579298)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:44:40.0 (3255271)|CODE_UNIT_FINISHED|OutREST.OutREST
17:44:40.0 (6483214)|EXECUTION_FINISHED
applicant.toString(1)
should beapplicant.toString()
?