1

I'm working on a trigger that will send an HTTP request on some records creation/updates.

I have developed the method the trigger should execute, however, every single HTTP request I send gets a 401 code. I can manually put the request in a browser and get a 200/201 code, it's just when it runs from salesforce that the request is 401.

I am sending an access token as a header, the token works, and the site I'm sending the request to is in the Remote Sites list on org>setup (I am using a trailhead playground)

What can I do to fix this? Is this a limitation of the trailhead playground or am I doing something wrong?

sample code

public static HTTPResponse getResource(){
        HttpRequest req = new HttpRequest();
        
        req.setEndpoint('https://someWebsite.com/api/v1/SeeResource?');
        req.setMethod('GET');
        String authToken = 'myToken==';
        req.setHeader('access_token', authToken);
        req.setHeader('format', 'json');
        
        Http http = new Http();
        HTTPResponse res = http.send(req);
        return res;
    }
1

Authorization in a header would be in the Authorization header. It appears that you meant to use query string parameters instead:

String authToken = 'myToken==';
PageReference ref = new PageReference('https://someWebsite.com/api/v1/SeeResource');
ref.getParameters.putAll(new Map<String, String> {
  'access_token' => authToken,
  'format' => 'json'
});
String endpoint = ref.getUrl();
HttpRequest req = new HttpRequest();    
req.setEndpoint(endpoint);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
return res;

However, without having some documentation, it's hard to tell what's going on.

I can say that there's a defined list of allowed headers, and they are these headers. Neither access_token nor format are acceptable; you'd have to prefix them with X- to be valid.

3
  • Thank you. I was actually mixing HTTP headers with params...I did mean to use params. I changed my code to reflect yours, however, I'm still getting unauthorized while in Postman I get a good request. This is the documentation for the API I'm querying dev.targetprocess.com/reference#userstories-get
    – ThePorcius
    Mar 23 at 19:42
  • I noticed that I had an unescaped character in my token, that was causing the issue! Thank you, it works now!
    – ThePorcius
    Mar 23 at 19:49
  • @ThePorcius Grat. Glad I could help a bit, at least.
    – sfdcfox
    Mar 23 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.