[SalesForce] Retrieve “Authorization” header using apex

I have an apex class which has access rights to a salesforce site

@RestResource(urlMapping='/testsite/*')
global with sharing class MyClass 
{
    @HttpPost
    global static void doPost()
    {
        RestRequest restRequest = RestContext.request;

        System.debug('======== Auth Header: ' + restRequest.headers.get('Authorization'));

    }
}

I'm using hurl.it to send post requests to this site. In the requests I'm setting the authorization header using Basic authentication but for some reason it seem to be always null.

Am I missing something here?

Best Answer

This is a duplicate of Authorization header missing from RestRequest headers map

Salesforce does not allow access to the Authorization header.

If you are using the a Salesforce login with OAuth use UserInfo.getSessionId() to get the Authorization data.

I created a public REST service that uses a shared key for authorization. As a workaround I added a custom header called AuthorizationToken. By checking to ensure this is present and valid I can authorize a call.

Related Topic