1

When I m calling that from POSTMAN with the access token,etc.. I m getting the response. But when I m calling this from Apex it is giving Unauthorized(status:401). I don't know why. See code below:

public static httpResponse callOut () {
    HTTP h = new HTTP();
    String httpMethod = 'GET';
    String accountId = 'xxxxxxxxxxxxxxxxxxxxxx';
    String authToken = 'xxxxxxxxxxxxxxxxxxxxxx';
    String endpoint = 'https://api.apifonica.com/v2/accounts/' + accountId ;
    httpRequest req = new httpRequest();
    String authValue = accountId + ':' + authToken;
    Blob headerValue = Blob.valueOf(accountId +':'+authToken);
    String authorizationHeader = 'Basic' + EncodingUtil.base64Encode(headerValue);
    req.setHeader('Authorization',authorizationHeader);
    req.setMethod(httpMethod);
    req.setEndpoint(endpoint);
    //req.setTimeout(120000);
    //Use the HTTP Class to send the httpRequest and receive an httpResponse
    httpResponse res = h.send(req);
    System.debug('response' + res.getBody());
    return res;
}

Here is the screenshot of Postman: enter image description here Thanks for helping!

  • Have you tested end point in Postman right? can you please share the screen shot!! – SFDC FAN Sep 7 '18 at 10:44
  • Yes, i updated the question – user7697691 Sep 7 '18 at 10:45
  • I think you're missing a space in your header, try: String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); – rael_kid Sep 7 '18 at 10:51
  • 1
    Okay..@RaeL_kid please answer the question.Its good for other viewers reference – SFDC FAN Sep 7 '18 at 11:03
2

You're missing a space in your authorization header string, use this instead:

String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.