Skip to main content
Tweeted twitter.com/#!/StackSalesforce/status/570730752247275520
deleted 16 characters in body
Source Link
BarCotter
  • 12.4k
  • 4
  • 38
  • 58

I have defined the endpoint and given the username and password for the authorization under named credentials (Spring 15 Feature),when I do a http Request I am getting Unauthorized(401)
HttpRequest req = new HttpRequest(); req.setEndpoint('callout:My_Named_Credential/some_path'); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBodyUnauthorized()401);

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:My_Named_Credential/some_path');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

Response : 401

But if I hardcode the end point,username and password in the request  ,I I am authorised (without using named credentials)

HttpRequest req = new HttpRequest(); req.setEndpoint('https://endpoint.example.com/some_path'); req.setMethod('GET'); // Specify the required user name and password to access the endpoint // As well as the header and header information String username = 'myname'; String password = 'mypwd';

Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader);

// Create a new http object to send the request object // A response object is generated as a result of the request

Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());

HttpRequest req = new HttpRequest();
req.setEndpoint('https://endpoint.example.com/some_path');
req.setMethod('GET');
// Specify the required user name and password to access the endpoint
// As well as the header and header information
String username = 'myname';
String password = 'mypwd';

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);

// Create a new http object to send the request object
// A response object is generated as a result of the request  

Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

Response : OK

Is that Named credential includes the Authorization Header automatically?

What am I missing here ?

I have defined the endpoint and given the username and password for the authorization under named credentials (Spring 15 Feature),when I do a http Request I am getting Unauthorized(401)
HttpRequest req = new HttpRequest(); req.setEndpoint('callout:My_Named_Credential/some_path'); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());

Response : 401

But if I hardcode the end point,username and password in the request  ,I am authorised (without using named credentials)

HttpRequest req = new HttpRequest(); req.setEndpoint('https://endpoint.example.com/some_path'); req.setMethod('GET'); // Specify the required user name and password to access the endpoint // As well as the header and header information String username = 'myname'; String password = 'mypwd';

Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader);

// Create a new http object to send the request object // A response object is generated as a result of the request

Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());

Response : OK

Is that Named credential includes the Authorization Header automatically?

What am I missing here ?

I have defined the endpoint and given the username and password for the authorization under named credentials (Spring 15 Feature),when I do a http Request I am getting Unauthorized(401)

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:My_Named_Credential/some_path');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

Response : 401

But if I hardcode the end point,username and password in the request, I am authorised (without using named credentials)

HttpRequest req = new HttpRequest();
req.setEndpoint('https://endpoint.example.com/some_path');
req.setMethod('GET');
// Specify the required user name and password to access the endpoint
// As well as the header and header information
String username = 'myname';
String password = 'mypwd';

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);

// Create a new http object to send the request object
// A response object is generated as a result of the request  

Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

Response : OK

Is that Named credential includes the Authorization Header automatically?

What am I missing here ?

Source Link
Pavan
  • 372
  • 2
  • 10

Working with Named Credentials spring 15

I have defined the endpoint and given the username and password for the authorization under named credentials (Spring 15 Feature),when I do a http Request I am getting Unauthorized(401)
HttpRequest req = new HttpRequest(); req.setEndpoint('callout:My_Named_Credential/some_path'); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());

Response : 401

But if I hardcode the end point,username and password in the request ,I am authorised (without using named credentials)

HttpRequest req = new HttpRequest(); req.setEndpoint('https://endpoint.example.com/some_path'); req.setMethod('GET'); // Specify the required user name and password to access the endpoint // As well as the header and header information String username = 'myname'; String password = 'mypwd';

Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader);

// Create a new http object to send the request object // A response object is generated as a result of the request

Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());

Response : OK

Is that Named credential includes the Authorization Header automatically?

What am I missing here ?