I am trying to rewrite the following Java HTTP request in Salesforce.

    String apiUrl = "url";
    String wsUser = "YourWSUser";
    String wsPassword = "YourWSPassword";

    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wsUser, wsPassword);
    provider.setCredentials(AuthScope.ANY, credentials);

    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

    HttpPost httpRequest = new HttpPost(apiUrl);
    httpRequest.addHeader("Content-Type", "application/json");
    httpRequest.setEntity(new StringEntity(paymentRequest.toString(), "UTF-8"));
    HttpResponse httpResponse = client.execute(httpRequest);
    String paymentResponse = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

This is what I wrote in APEX:

    HttpRequest req = new HttpRequest();


    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
    req.setHeader('Authorization', authorizationHeader);

    req.setHeader('Content-Type', 'application/json');

    Http http = new Http();
    HTTPResponse res = http.send(req);

My questions are:

  • Is the conversion correct?
  • If so any idea why I get a 401 as if my credentials are not set correctly. (knowing that the credentials and end point is correct)
  • 1
    Possible duplicate of 403 - Forbidden Cannot authenticate user Commented Apr 18, 2016 at 16:19
  • 2
    @VigneshwaranG No, it's not a duplicate of that question.
    – sfdcfox
    Commented Apr 18, 2016 at 16:25
  • @VigneshwaranG It's a completely different question I am getting here a 401 Unauthorized error code
    – raym0nd
    Commented Apr 18, 2016 at 17:24

1 Answer 1

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

This code is not correct. The automatic "toString" method results in a different message than you expect:

Blob b = Blob.valueOf('World');
System.debug('Hello '+ b);


11:21:43.1 (2562345)|USER_DEBUG|[2]|DEBUG|Hello Blob[5]

Instead, you do want to use a regular string:

String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(username + ':' + password);

As an aside, you can't provide a Body when method is set to GET. The platform will implicitly coerce the request to a POST instead. You should always make sure you're using the correct HTTP verb, because some servers will throw random fits if you don't follow the allowed verbs/request style.

  • I got that the blob is different.. but I took my code from Salesforce .. from here Anyways I will change and give it a try.
    – raym0nd
    Commented Apr 18, 2016 at 16:27
  • 1
    @raym0nd Hmmm... Looks like I was indeed wrong, after all. What's disturbing though is that the salesforce code shouldn't be wrong (but probably is). Further investigation is necessary. Give me a few minutes.
    – sfdcfox
    Commented Apr 18, 2016 at 16:31
  • I changed it to POST, it still has the same issue. Btw my username and passwords are both in string...
    – raym0nd
    Commented Apr 18, 2016 at 16:34

You must log in to answer this question.

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