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
David MassadDavid Massad 

Named Credentials and Basic Authentication

I am having a problem with using named credentials for an HTTP callout.  When I follow the example in the article titled, "Spring '15 - Simplify Authenticated Apex Callouts by Specifying Named Credentials as Endpoints" (https://help.salesforce.com/HTViewSolution?id=000206761&language=en_US" target="_blank), I get a 401 Unauthorized error from the server I'm trying to connect to.
I used Runscope to inspect the headers being sent to the server from Salesforce, and it seems that using named credentials is leaving off the "Basic" prefix from the Authorization header's value.  For example, if your username is "abc" and your password is "123", using named credentials sends this in the header:
Authorization: YWJjOjEyMw==
Instead, it should be sending this for it to work:
Authorization: Basic YWJjOjEyMw==
Is this a bug with Named Credentials?
KevinPKevinP
Can you share the code you're using to make the call? and a screen shot showing your named credential setup? Trying to duplicate here.
BDatlaBDatla
Hi David,

Yes, I think it is a bug with Named Credentials in the article you shared.
The below code is working for me :
        Blob headerValue = Blob.valueOf(username + ':' + password);
         String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
         req.setHeader('Authorization', authorizationHeader);

Let me know if this any help for you ?

Cheers.
BD
Douglas AyersDouglas Ayers
This is a bug, using Named Credentials is missing the keyword "Basic " in the auth header value. I opened a case with Support a month ago and went through the examples with them then they finally closed my case as "bug fix submitted" but have not yet provided me link to the Known Issue page.
Michael GuseMichael Guse
The bug has been fixed and the Authorization header now correctly contains the keyword 'Basic' in the Authorization header.
Somnath Banik 9Somnath Banik 9
I am having issue when wroking with custom setting the encode value gemnerated which is difrance when using Named credential . Any Idea why it is same although user name and passowrd in Custom setting and Name credential are same . 

public String endpoint_x = 'Callout:Availability****';
...
WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,);
response_x = response_map_x.get('response_x');
            return response_x;

**************
// adding authorization
        String authorizationHeader = 'BASIC ' +
            EncodingUtil.base64Encode(Blob.valueOf(objAuthorization.username + ':' + 
                objAuthorization.password));