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
SaranSaran 

issue with set header() in request method

Hi All,

I have a question in sending a callout.

My Endpoint is : 'http://www.XXXXXXX.YYYYYY.NNN/ZZZZ/MMMM/'

Uer id and pssword as set a header.

So my request will be like,

HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.XXXXXXX.YYYYYY.NNN/ZZZZ/MMMM/');
req.setMethod('GET');
req.setHeader('username', 'AAAAAAAAA');
req.setHeader('password', 'QQQQQQQQQ');

Http http = new Http();
HttpResponse res = http.send(req);
system.debug('$$$$$' + res.getBody());

When i try this i am getting error someting invalid.

But the strange thing is when i directly appending it to url its working good.

HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.XXXXXXX.YYYYYY.NNN/ZZZZ/MMMM/?username=AAAAAAAAA&password=QQQQQQQQQ');
req.setMethod('GET');

Http http = new Http();
HttpResponse res = http.send(req);
system.debug('$$$$$' + res.getBody());

Now in the debug i getting the correct response.

Can anyone tell me why i cant use setHeader Method.


Thanks,
RAM AnisettiRAM Anisetti
Hi,

Use authentication like this....
 
String username = 'Ram'; 
 String password = 'RamS'; 
 Blob headerValue = Blob.valueOf(username + ':' + password); 
 String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); 
​ req.setHeader('Authorization', authorizationHeader);