0

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 enter image description here

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 :(

enter image description here

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.

enter image description here

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');
6
  • What is the oauth endpoint URL that you are requesting ? Jan 29, 2016 at 2:35
  • @MohithShrivastava I have updated the question with my inputs. Can you take a look ?
    – Rainmaker
    Jan 29, 2016 at 2:47
  • What is your oauth end point please ?Unless you hit proper oauth end point its not going to work Jan 29, 2016 at 3:55
  • 1
    @kvnfv The response you were getting is in Japanese. That's probably because your destination org's language preference is set to Japanese (which would make sense, as the ap2 instance serves the Asia-Pacific region). I can't pull much from the error, but it's saying "the URL does not exist"
    – Derek F
    Jan 29, 2016 at 13:54
  • @kvnfv Sounds like the original URL you were using was not correct, and you corrected it. What was the url that was originally printed out by System.debug()? Could you post an answer with what you needed to do?
    – metadaddy
    Jan 29, 2016 at 17:08

0

You must log in to answer this question.

Browse other questions tagged .