Introduction:
In Salesforce development, handling file uploads and multipart data is a common requirement. Multipart data allows you to send multiple types of data in a single HTTP request, including files and form data. The boundary is a unique identifier used to separate the different parts of the multipart request. In this blog post, we will delve into multipart data, boundaries, and provide an example code snippet in Salesforce to help you understand their implementation.
1. What is Multipart Data?
Multipart data is a way of packaging multiple pieces of data into a single HTTP request payload. Each part of the data can have its own content type and content disposition. It is commonly used for file uploads, where the file content is one part of the multipart data, and other form data fields can be additional parts.
2. What is the Boundary?
The boundary is a string that acts as a separator between the different parts of the multipart data. It helps the server identify the start and end of each part. The boundary string should be unique and not present in the content of any part.
Example Code in Salesforce:
Let's take a look at an example code snippet in Salesforce that demonstrates how to handle multipart data and boundaries while uploading a file.
public class MultipartUploader {
public void uploadFile(Blob fileBlob, String fileName) {
String boundary = '---BOUNDARY---' + DateTime.now().getTime();
String contentType = 'multipart/form-data; boundary=' + boundary;
HttpRequest req = new HttpRequest();
req.setEndpoint('https://your-salesforce-instance.com/services/data/v53.0/sobjects/ContentVersion');
req.setHeader('Content-Type', contentType);
req.setHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN');
req.setMethod('POST');
String body = '';
body += '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="file"; filename="' + fileName + '"\r\n';
body += 'Content-Type: application/octet-stream\r\n';
body += '\r\n';
body += EncodingUtil.base64Encode(fileBlob) + '\r\n';
body += '--' + boundary + '--';
req.setBody(body);
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 201) {
System.debug('File uploaded successfully!');
} else {
System.debug('File upload failed. Error: ' + res.getBody());
}
}
}
Explanation:
- The uploadFile method takes the file's content as a Blob and its fileName as parameters.
- We generate a unique boundary string by appending the current timestamp to a constant string.
- The contentType header is set to specify the multipart form data with the generated boundary.
- An HttpRequest is created with the necessary headers and endpoint.
- The body of the request is constructed by appending different parts with their corresponding headers and content.
- The file content is Base64 encoded before adding it as a part of the request body.
- Finally, the request is sent using the `Http` class, and the response is checked to determine the success or failure of the upload.
Conclusion:
Understanding multipart data and boundaries is crucial for handling file uploads and form data in Salesforce. By leveraging multipart data, you can efficiently send multiple types of data in a single HTTP request. The example code provided in this blog demonstrates a practical implementation of uploading a file using multipart data and boundaries in Salesforce.