I have tried with the below code but have received {"error":"invalid_request","error_description":"Missing parameters: password"} in response. I have tried with POSTMAN and getting correct response.

What is missing here:

Http http = new Http();
HttpRequest request = new HttpRequest();
String reqUrl = 'https://api.abc.com/token';
request.setHeader('Authorization','Basic T2dfT2Q2V1R3OHRkYUhyTkN6WlNwaVk0YldRYTpVW');
String payLoad = 'grant_type=password&[email protected]&password=O$Q3tz%Hdkqq&scope=PRODUCTION';
System.HttpResponse response = new System.Http().send(request);

1 Answer 1


I think you need to URL-encode the parameters in your payload. The password needs it in this instance, and it's worth doing for the username too.

So, you would have:

String payLoad = 'grant_type=password' 
+ '&username=' + EncodingUtil.urlEncode('[email protected]','UTF-8') 
+ '&password=' + EncodingUtil.urlEncode('O$Q3tz%Hdkqq', 'UTF-8') 
+ '&scope=PRODUCTION';

More generally, I tend to pass the params in as a map, and then write a utility function which url encodes all of the keys and values as it turns them into a query string.

global static String urlEncode(Map<String, String> vals) {
    String result = '';
    for(String thisKey : vals.keySet()) {
        result += EncodingUtil.urlEncode(thisKey, 'UTF-8') + '=' + EncodingUtil.urlEncode(vals.get(thisKey), 'UTF-8') + '&';
    return result.removeEnd('&');    
  • Sweet! URL-encode works for me. Just one question: I had earlier tried to urlEncode the complete PayLoad string but that didn't work. Should I only do urlEncode for username and password, but not the complete string.
    – devforce
    Commented Jan 31, 2018 at 7:24
  • 1
    You can't apply it to the whole string, otherwise it turns the & characters which are supposed to be the separator between arguments into %26 and the = into %3D
    – Aidan
    Commented Jan 31, 2018 at 7:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .