I want to send REST request from one SF org to another. Is there any REST Client class/s which can handle the authentication and communication for me or I need to write my own classes?

3 Answers 3


While this won't handle the authorization (oAuth setup) bit, here's a gist that you can use as a basic RestClient. This is a Virtual class, so you'll need to create your own class and extend this rest class. Once you do that, you'll have access to Get(), Put(), Post(), etc. Including the ability to set Oauth and Authorization headers needed for Salesforce Rest API calls.

If you're interested in collaborating on building an oAuth enabled RestClient for SF, let me know.

RestClient Gist

  • I think this is not correct.He wants to connect salesforce with salesforce using apex Oct 21, 2013 at 13:25
  • Mohith, that is an apex class. i just titled it .java so gist would give it some syntax highlighting. Rest assured, this is an Apex class
    – Kevin P
    Oct 21, 2013 at 13:28
  • My bad !.java confused me .Nice work .where is oauth in this from sfdc to sfdc? Oct 21, 2013 at 13:53
  • As I said in my original post, this doesn't handle the Authorization (oAuth) bit. but it could be used to build the oauth bit.
    – Kevin P
    Oct 21, 2013 at 13:58
/*End point Url to web service callout*/
private final static String ENP_POINT_URL ='https://login.salesforce.com/services/oauth2/token';
//For development and production https://login.salesforce.com/services/oauth2/token
//And for sandbox https://test.salesforce.com/services/oauth2/token
private final static String CONSUMER_KEY = 'Your_Org_Consumer_Key';
private final static String CONSUMER_SECRET = 'Your_Org_Consumer_Secret';
  private final static String USERNAME = 'Your_Username';
private final static String PASSWORD = 'Your_Password';
private final static String REQUEST_BODY = 'grant_type=password&client_id={0}&client_secret=

/*To generate Access token Method*/
private static OAuth getAccessToken(){
        HttpRequest req = new HttpRequest();
        Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
        String authorizationHeader = 'BASIC ' +
        req.setHeader('Authorization', authorizationHeader); 
        req.setBody(String.format(REQUEST_BODY ,new string[]{CONSUMER_KEY,CONSUMER_SECRET,
        Http http = new Http();
        HttpResponse res = http.send(req);
        OAuth objAuthenticationInfo = (OAuth)JSON.deserialize(res.getbody(), OAuth.class);
        return objAuthenticationInfo;
    }catch(CallOutException ce){
        throw ce;
    return null;

/*To get Access token property*/
public static OAuth authenticationDetail{
        if(authenticationDetail == null){
            authenticationDetail = getAccessToken();
        return authenticationDetail;

/*To get aouthentication detail Wrapper*/
public class OAuth{
    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;}    

I agree with above answers but if you need the code just for username-password flow i pulled this to help you

  • Very helpfull, I will surely use it
    – Dedo
    Oct 21, 2013 at 21:02
  • what should I put instead of REQUEST_BODY?
    – Dedo
    Oct 21, 2013 at 21:37
  • added the missing code !Sorry about that missed important lines while pasting Oct 22, 2013 at 4:29

If you rely on static credentials instead of a dynamic authorization for different users I'd recommend to use the SOAP login call (here) instead of a full OAuth client implementation. It's manually built in minutes and you can use the SessionId from the LoginResult response in your REST calls as well. A mixture of protocolls for the sake of simplicity.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .