function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GAURAV SETHGAURAV SETH 

External Web Service Call from Apex Class-Certificate Issue

I need to call a external web service from Apex class.
I am always getting unsuccessful response.
If I hit that url from Postman, I am getting response successfully.
I took the jSessionId LTPAToken2 from Postman response and add it to my Apex class as below:

request.setHeader('Cookie', 'JSESSIONID=0000huO7wL9GJGO_ZEAlW-4NDCd:-1; LtpaToken2=ZLjH0oX2UiZfYXakvwH5+AhhhCYaEIEEoBCsD12bHY+Ovs+4VF5zFo0B00gMDNjQMwcA==')

After this when I execute my Apex class, I am getting successful response. I am wondering that there migh tbe certificate issue .
Can someone please help ?
ayu sharma devayu sharma dev
Hello Gaurav

Can you please tell us how you are building your HTTP object? It is hard to say what mistake you are doing from this line.

Thanks 
Ayush
GAURAV SETHGAURAV SETH
Hi Ayush,
Here is the code used by me :

Http http = new Http();
        HttpRequest request = new HttpRequest();
        String intake_Id = intakeId[0];
        String EndPoint = 'https://URL:port/Rest/v1/cwa/intakes?investigation_id='+intake_Id;
        request.setEndpoint(EndPoint);
        request.setMethod('GET');

        HttpResponse response = http.send(request);

Thanks,
Gaurav Seth
ayu sharma devayu sharma dev
hello Gaurav

I can't see any Authorization header in your given HTTP object. 

As I can see '0000huO7wL9GJGO_ZEAlW-4NDCd:-1; LtpaToken2=ZLjH0oX2UiZfYXakvwH5+AhhhCYaEIEEoBCsD12bHY+Ovs+4VF5zFo0B00gMDNjQMwcA==' is a base64. So if you have any Secret or API key which you use, to authorize to the server then convert them into Base64 and use it inside your authorization header. 

If answer is not relative then please share the API documentation from where you have taken reference.

Thanks 
Ayush
GAURAV SETHGAURAV SETH
This is the part where I am adding authorization header.

       string username = 'user'; 
        string password = 'password';
         request.setTimeout(120000);
        
        // Add basic authentication to header
        // Create blob of user:pass
        Blob headerValue = Blob.valueOf(username + ':' + password);
        // Base 64 Encode the blob and prepend "Basic "
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        // Add the basic auth string to the Request Header
          request.setHeader('Authorization', authorizationHeader);

I have that external web service entry in Remote Site Settings too. Anything else needs to be done?

Thanks,
Gaurav Seth
GAURAV SETHGAURAV SETH
Can someone please assist me resolving the issue ?