0

I have a requirement to integrate Salesforce with external system, the Endpoint APIs are provided and i am using Named Credentials for making the callouts using Apex:

Below is the details for Authentication ENDPoint:

  • URL : www.example.com/authenticate
  • Method : POST
  • Header : apiKey : "afdsfgfddkgk"
  • Username : admin
  • Password : admin

I created a Named Credentials with the URL provided with below configuration enter image description here

and

After authentication i have to make a call to the below Product Details URL to fetch the details

  • URL : www.example.com/v1/pickup-requests

Note there is a slight difference in the path, my product details endpoint doesn't have authenticate in the path where as Named Credentials has.

When making the callout i am using the below Apex code:

   HttpRequest req = new HttpRequest();
   req.setEndpoint('callout:ApiCredentials/v1/pickup-requests');
   req.setMethod('GET');
   Http http = new Http();
   HTTPResponse resp = http.send(req);

When i debug the log the Endpoint changes to www.example.com/authenticate/v1/pickup-requests.

Question : Is there a way to get rid of path /authenticate while making callouts.

Thanks in advance

1 Answer 1

0

Don't include "authenticate" in the Named Credentials URL. Then, you can change your authorization call to: callout:ApiCredentials/authenticate. It's generally advisable to not include a path in the Named Credential unless the entire API is located under that path.

2
  • Do we need to make call to callout:ApiCredentials/authenticate each time we callout to other services to receive response. Doesn't Named Credentials take care of authentication each time we call other services in my case callout:ApiCredentials/v1/pickup-requests May 5, 2020 at 11:33
  • @GouravBhardwaj It depends on your scenario, different authentication methods are different. Since you're using Generate Authorization Header, it seems like you shouldn't need to call /authenticate at all, as the authentication should already be there. You would need to check the documentation for the API you're calling to determine the correct method for authentication.
    – sfdcfox
    May 5, 2020 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .