I am trying to integrate two SF dev boxes.
I have created connected app/remote site settings etc..and now I am at the stage where I am issuing a HTTP request from my Source SF org to Target SF org.
I can obtain the bearer token successfully but I am getting weird response when passing the payload to a webservice in my destination org.
The webservice in the destination org
@RestResource(urlMapping = '/Account/*')
global with sharing class AccountWS
{
@HttpPost
global static String createAccount(String nam)
{
Account a = new Account(Name = nam);
insert a;
return a.Id;
}
}
I was able to successfully insert an account using this WS from REST Explorer (Workbench).
Screenshot below
My anon apex which I execute from source org.
public class OAuth2
{
public String id{get;set;}
public String issued_at{get;set;}
public String instance_url{get;set;}
public String signature{get;set;}
public String access_token{get;set;}
}
RESTCreds__c r = RESTCreds__c.getValues('1');//Custom settings contains all credentials
String clientId = r.ConsumerKey__c;
String clientSecret = r.ConsumerSecret__c;
String userName = r.Username__c;
String password = r.Password__c;
String url = r.URL__c;
System.debug(clientId);
System.debug(clientSecret);
System.debug(userName);
System.debug(password);
System.debug(url);
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
HTTP h = new HTTP();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndPoint(url);
HttpResponse res = h.send(req);
System.debug('Response : '+res);
System.debug('Response Body : '+res.getBody());
OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(res.getBody(),OAuth2.class);
System.debug(objAuthenticationInfo);
System.debug('Id = '+objAuthenticationInfo.id);
System.debug('Issued At = '+objAuthenticationInfo.issued_at);
System.debug(' Instance URL = '+objAuthenticationInfo.instance_url);
System.debug(' Signature = '+objAuthenticationInfo.signature);
System.debug(' Access Token = '+objAuthenticationinfo.access_token);
String jsonstr = '{"nam":"sdsd"}';
System.debug(jsonstr);
if(objAuthenticationInfo.access_token != null)
{
Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setBody(jsonstr);
req1.setMethod('POST');
req1.setEndpoint(url+'/services/apexrest/Account');
HttpResponse res1 = h1.send(req1);
System.debug('Response for Payload : '+res1.getBody()); //WEIRD RESPONSE HERE
}
The response looks like some Chinese or Korean characters and I do not know why :(
The strange thing is when I do a mock callout via Advanced Rest Client from Chrome (it is a tool similar to CURL) I am able to get the desired output as shown below.
UPDATE :
@Mohith
I followed your answer in this post
How can i integrate one SFDC org to another SFDC using Rest Api
req1.setEndpoint(ts.URL__c+URL);//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
It was not working. Either I got the weird chinese message or I got the URL not reset error.
When I changed the endpoint to "objAuthenticationinfo.instance_url+rest api end point" it is working now.
This is as below
req1.setEndpoint('https://ap2.salesforce.com/services/apexrest/Account');