I'm trying to make use of Salesforce Named Credentials to securely store the API Key and call endpoint accordingly.
I tried to pass a custom API Key to an endpoint as below.
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('callout:API_endpoint'+'/customers');
req.setHeader('Authorization', '{!$Credential.Password}'); // Password contains 'Basic <<Base64EncodedKeyValue>>'
HTTPResponse res = new HTTP().send(req);
System.debug('res '+res.getBody());
But it's throwing the 401 - Unauthorized response.
- Is there a way to check the final request code e.g. like in postman request code?
- If I use the hard-coded http request attributes + remote site (without using named credential), it's working perfectly. Is it possible to override Authorization ? and How do I use Named credential to override Authorization header correctly?
Much appreciate any help/comment/thoughts on this? Cheers!