You need to sign in to do that
Don't have an account?
Mahesh 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();
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();
https://help.salesforce.com/HTViewHelpDoc?id=remoteaccess_authenticate.htm
"OAuth 2.0 username and password" flow should work for you.