+ Start a Discussion
SaranSaran 

setHeader() is not working

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,

post ur values to endpoint like below

String uname = 'Ram';

String pwd = 'RamS';

req.setBody('username='+EncodingUtil.urlEncode(+uname, 'UTF-8')+'&password='+EncodingUtil.urlEncode(+pwd), 'UTF-8'));