+ Start a Discussion
Mahesh SirmewarMahesh Sirmewar 

How to set header as user name and password in post method

I am developing a batch class to make api calls and get data from that api in to salesforce. I need to send http request by post method, with user name & password as header. I have tried but i got error. Can anyone please help me to do this. Below is my code please check it. (Error:Invalid Request error was encountered while trying to process the request)


HttpRequest req= new HttpRequest();
req.setMethod('POST');
String username = 'ABC';
String password = 'Sales123';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http http = new Http();
String url = 'http://XYZ';
req.setEndpoint(url );
HttpResponse res = http.send(req);
return res.getBody();
 
AshwaniAshwani
I think Salesforce no more support this flow only available authenitcation flows are:

https://help.salesforce.com/HTViewHelpDoc?id=remoteaccess_authenticate.htm

"OAuth 2.0 username and password" flow should work for you.