[Solved] Retrieve data from external system using HTTP calls

user5352 Asks: Retrieve data from external system using HTTP calls
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());



Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.