I am trying to call an endpoint in APEX which requires Authorization and the body which it expects is multi/form-part. I am getting evertytime access denied. I have tried using postman and the endpoint is fine. below is my code.

 public void APICALL()

     HttpRequest req = new HttpRequest();
     String endPointURL = 'https://abcwerr.ffff.com/v2/ttttt';
    String userName = 'BlahBlah';
    String password = 'BlahBlah';
   Blob headerValue = Blob.valueOf(userName + ':' + password);
    String authorizationHeader = 'Basic' +
    req.setHeader('Content-Type', 'multipart/form-data');
    string bdy = 'merchant_url='+ AppForm.Business_Website__c;

    Http http = new Http();

    HTTPResponse res = http.send(req);
    this.SpiderResponse = res.getBody(); 

multipart/form-data requires a boundary between the parts, even if there is just one part.

See Steps required to support POSTing multipart/form-data Content-Type from Apex

You also need a space between the 'Basic' and encoded value in the header. E.g.

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
|improve this answer|||||

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.