+ 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);