0

As the title suggests, whenever I try to send an API request with a PUT method and a multipart/form-data as the content-type, to an Azure Data Store, I get a 403 Forbidden response back.

I am using a named credential (with valid Auth Providers for authentication), and get the endpoint directly from it. The data is in JSON format, and I am not sending it as an application/json because the contents are too big; it was suggested that I submit it as a json within a multipart, so Azure identifies it as a file and applies different request size limits.

With the exact same credentials, I am able to use Postman. But on Salesforce, it fails. The contents of the body are exactly the same on both cases. The headers are similar, but the important ones are the same, e.g.: content-length, x-ms-version, x-ms-blobtype and content-type.

Here's some sample Apex code, on the Salesforce-side, that I use to make the request.

String fileName = 'a_file.json';
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:MyNamedCredential'+ '/path/' + fileName);
request.setMethod('PUT');
String boundaryString = 'aBoundaryString';
String fileHeader = 'Content-Disposition: form-data; name="file"' + '; filename="' + fileName + '"' + '\nContent-Type: application/json';
String bodyMultipartFormData = '--' + boundaryString + '\n' + fileHeader + '\n\n' + body  + '\n' + '--' + boundaryString + '--';
request.setBody(bodyMultipartFormData);
request.setHeader('Content-Type','multipart/form-data; boundary='+boundaryString);
request.setHeader('x-ms-blob-type','BlockBlob');
request.setHeader('x-ms-version','2021-06-08');
// Make the request
1

1 Answer 1

0

I eventually figured it out. As I was sending the request to Azure DataStore, most likely they are interpreting it in a windows-based machine. Whenever I added LF (\n), I wasn't accounting for the fact that they instead use CRLF which translates to an extra \r char, resulting in \r\n whenever they interpret a new line.

I guess, moral of the story, be mindful of the machine that you are going to make requests to.

You must log in to answer this question.

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