1

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' +
    EncodingUtil.base64Encode(headerValue);
     req.setEndpoint(endPointURL);
    req.setHeader('Content-Type', 'multipart/form-data');
    req.setHeader('Authorization',authorizationHeader);
    req.setMethod('POST');
    string bdy = 'merchant_url='+ AppForm.Business_Website__c;

    req.setbody(EncodingUtil.base64Encode(bdy)); 
    Http http = new Http();

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

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.