I have created SF to SF connection to put standard object cross org, getting error related to JSON response as 400 Bad Request. Error as: Unexpected parameter encountered during deserialization: LastName....' When im uncommenting line 'Commented 1' it works well with hardcoded values in JSON but fails when i put the field names it fails.
String endPoint='https:aaa//';
Commented 1//String jsonstr='{"Lastname":"Testname123","Phone":"5565565","Website":"www.xyz.com","Status":"Open","Company":"1company"}';
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('LastName', Lastname);
gen.writeStringField('Phone', Phone);
gen.writeStringField('Website', Website);
gen.writeStringField('Status', status);
gen.writeStringField('Company', company);
gen.writeEndObject();
// Get the JSON string.
String jsonstr = gen.getAsString();
String str1= jsonstr;
system.debug(jsonstr); //this line displays proper JSON format(with the field values)
Http h2= new Http();
HttpRequest req2= new HttpRequest();
req2.setHeader('Content-Type','application/json; charset=utf-8');
req2.setHeader('Authorization','Bearer ' + accessToken);
req2.setBody(gen.getAsString());
req2.setMethod('POST');
req2.setEndpoint(endPoint);
HttpResponse res2=h2.send(req2);