I am trying make a API call to an external system (CPQ) from the Salesforce.

  1. I am able to successfully login to the external system through http post
  2. After successful login, now I want to query Account object on the external system and retrieve the results
  3. How do i make a query to the external system using Get method. When I make a external call, I am getting Unauthorized error.

HttpResponse[Status=Unauthorized, StatusCode=401]

Updated code:

public void cpqCallOut() {

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();
    String strname = 'Username';
    String strpwd= 'password';

     Blob headerValue = Blob.valueOf(strname+':'+strpwd);
            String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);

    String SessionId = Userinfo.getSessionId();
    String strURL = System.URL.getSalesforceBaseURL().toExternalForm();

    req.setHeader('Authorization', authorizationHeader);
    req.setBody('un ='+EncodingUtil.urlEncode(strname, 'UTF-8') + 'pw =' + EncodingUtil.urlEncode(strpwd, 'UTF-8') +
    'SfdcSessionID  =' + EncodingUtil.urlEncode(SessionId, 'UTF-8') + 
    'SfdcServerURL ='+  EncodingUtil.urlEncode(strURL , 'UTF-8'));
    req.setCompressed(true); // otherwise we hit a limit of 32000

    String strQuery = 'Select+name+from+Account+where+id+=+158000000000010'; 

    //new request object
    HttpRequest req1 = new HttpRequest();
    HttpResponse res1 = new HttpResponse();
    Http http1 = new Http();

    try {
        res = http.send(req);

     if(res.getStatusCode()== 200)

           req1.setEndpoint('https://sbx.fpx.com/rs/8/cpq?query='+ strQuery);
           res = http.send(req1);

           System.debug('MY REQUEST===>'+req1.toString());
           System.debug('RESPONSE FROM CPQ 2222'+res.toString());

    } catch(System.CalloutException e) {
        System.debug('Callout error: '+ e);
        System.debug('RESPONSE FROM CPQ'+res.toString());




To know the error cause you need to check following points:

  1. What is the authorisation method of external system like basic auth or any custom solution
  2. What is the external system? is it salesforce?
  3. If it is using basic auth, then you need to send cridentials like below:

Blob headerValue = Blob.valueOf(username+':'+password); String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); request.setHeader('Authorization', authorizationHeader);

If you are able to login successfully, then you need to create a new http request object because GET method can not have body attribute.

Try these steps, I hope this will solve your problem. Let me know if you need more help.


  • Thank you so much for your response. The external system is CPQ (fpx vendor). Below is the API document that was shared docs.fpx.com/docs/api/restful/14/html/cpq_login_(post).html I modified the code with the header and pasted it in my original question, the login is still successful, after the successful login, when i try to use a new request object to query the CPQ system, I am getting: System.HttpResponse[Status=Unauthorized, StatusCode=401] – user5352 Jan 20 '16 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.