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
Abhishek Raj 13Abhishek Raj 13 

Http rest callout 401 error

Hi,

While calling out other system's webservices from salesforce org. This 401 invalid seesion id  error is being prompted. Even though i had mentioned Username and password in header of HttpRequest. Is there any workaround to fix it.

Code snippet has been attached below.


Http h = new Http();
Httprequest request = new Httprequest();
request.setEndpoint('https://ap2.salesforce.com/services/apexrest/Accounts/0012800000XNqPO/contacts');
request.setMethod('GET');
String username = 'xxxxx';
String password = 'xxxx';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
Httpresponse response = h.send(request);
System.debug(response.getBody());




Any help, would be appreciate.

Thanks,
Abhi

@Karanraj@Karanraj
Check this article to connect salesforce org to salesforce using name crendtial which uses oauthentication process to connect.
http://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/
sslodhi87sslodhi87
Hi Abhishek,

Can you please check by appending the security token with password and send the request.

Thanks