I am trying make a API call to an external system (CPQ) from the Salesforce.
- I am able to successfully login to the external system through http post
- After successful login, now I want to query Account object on the external system and retrieve the results
- 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.setEndpoint('https://sbx.fpx.com/rs/8/cpq/login');
req.setMethod('POST');
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)
{
System.debug('SUCCESS!!!');
req1.setMethod('GET');
req1.setEndpoint('https://sbx.fpx.com/rs/8/cpq?query='+ strQuery);
req1.setCompressed(true);
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());
}
}
}