Sending Form-Data in Apex Callout to Auth0?

Question:
I need to send a multipart/form-data request from Salesforce Apex to Auth0’s /api/v2/jobs/users-imports
endpoint. I have limited experience working with form-data requests.
I structured the request body as follows:
--usersjiaosd9821931-FGGHJ
Content-Disposition: form-data; name="users"; filename="users.json";
Content-Type: application/json
[{"email": "ian+3@gmail.com","password": "alwaysPostFakePasswords"}, {"email": "ian+4@gmail.com","password": "sdfnDAqpAdoQF&www344"}]
--usersjiaosd9821931-FGGHJ
Content-Disposition: form-data; name="connection_id"
con_test
--usersjiaosd9821931-FGGHJ--
My Apex code:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('The url to /api/v2/jobs/users-imports');
request.setMethod('POST');
request.setHeader('Accept', 'application/json');
request.setHeader('Content-Type', 'multipart/form-data; boundary=--usersjiaosd9821931-FGGHJ');
request.setBody(body);
HttpResponse response = http.send(request);
System.debug(response.getBody());
However, I keep getting this error response:
{"statusCode":400,"error":"Bad Request","message":"Must provide 'users' file as multipart part.","errorCode":"invalid_body"}
What is wrong with my request, and how can I properly format it in Apex?
Answer:
The issue is likely due to improper formatting of the multipart request in Apex. Unlike other content types, multipart/form-data
requires properly structured boundaries, correct handling of file uploads, and proper encoding of each part.
CRS Info Solutions offers expert Salesforce online training with real-time projects, certification guidance, interview coaching, and a job-ready approach. Enroll for free demo today!!!
Use Blob
for File Upload
Apex requires setting the body using Blob
when sending files in multipart/form-data
. Try this approach:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('The url to /api/v2/jobs/users-imports');
request.setMethod('POST');
String boundary = 'usersjiaosd9821931-FGGHJ';
request.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
String body = '--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="users"; filename="users.json"\r\n' +
'Content-Type: application/json\r\n\r\n' +
'[{"email": "ian+3@gmail.com","password": "alwaysPostFakePasswords"}, ' +
'{"email": "ian+4@gmail.com","password": "sdfnDAqpAdoQF&www344"}]' +
'\r\n--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="connection_id"\r\n\r\n' +
'con_test\r\n' +
'--' + boundary + '--';
request.setBodyAsBlob(Blob.valueOf(body));
HttpResponse response = http.send(request);
System.debug(response.getBody());
Explanation: The code constructs a multipart/form-data
HTTP request in Apex to send a user import request to Auth0. It defines a boundary string to separate form fields, formats the request body correctly with Content-Disposition
headers, and includes both a JSON file (users.json
) and a connection_id
parameter. Finally, it converts the body to a Blob
using setBodyAsBlob
to ensure proper encoding before sending the request.
Summing Up
When sending multipart/form-data
from Apex to Auth0, ensure correct boundary formatting, proper content disposition, and use setBodyAsBlob
for file uploads. The error occurs because the API expects a well-structured file part, which requires precise \r\n
line breaks and encoding. Fixing the request involves correctly structuring the body, using a Blob
, and ensuring the right headers. Properly formatted requests prevent 400 Bad Request
errors and ensure seamless API communication.
Accelerate Your Salesforce Career with Expert Training in India
Take your professional growth to new heights with CRS Info Solutions’ premier Salesforce training. Designed to equip you with the essential skills and hands-on experience, Salesforce training in India our comprehensive courses cover Salesforce Administration, Development, and AI-driven modules, ensuring you’re ready to excel in the dynamic Salesforce ecosystem.
Our industry-focused approach combines in-depth theoretical learning with real-world projects, helping both beginners and experienced professionals master key concepts and best practices. With expert-led certification preparation, personalized mentorship, and extensive study materials, you’ll gain the confidence to ace interviews and thrive in your Salesforce career.
Start your journey today—sign up for a free demo class and take the first step toward a successful future in Salesforce!