function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Somya TiwariSomya Tiwari 

Sending files as MultiPart data in Salesforce

I am integrating with one of the provider who accepts a file to upload in their system. While doing so they require the file to be sent as multipart/form-data in the following format !! 

I tried configuring it as MultiPart form data using Base64 String still it is not working !! 

-H "accept: application/json"
-H "x-api-key: API_KEY" 
-H "x-api-token: API_TOKEN" 
-H "Content-Type: multipart/form-data" 
-F "file=@b4b35527-522d-4228-a266-bdaa80e28a8b.jpg;type=image/jpeg"
AnudeepAnudeep (Salesforce Developers) 
Hi Somya, 

I recommend reviewing the following example

POST Mutipart/form-data with HttpRequest

Anudeep
V TV T

You can check these two article. 

Apex Code for Uploading Files Using Multipart/Form Data in Salesforce (https://conveet.blogspot.com/2023/06/apex-code-for-uploading-files-using.html)

Upload file using multipart/form data from apex salesforce (https://conveet.blogspot.com/2023/06/upload-file-using-multipartform-data.html)

V TV T
public class FileUploadController {
    public void uploadFile(String fileName, Blob fileData) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://your-endpoint-url.com/upload'); // Replace with your desired endpoint URL
        req.setMethod('POST');
        
        String boundary = '----------------------------' + String.valueOf(System.currentTimeMillis());
        String header = '--' + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + fileName + '"\r\nContent-Type: application/octet-stream\r\n\r\n';
        String footer = '\r\n--' + boundary + '--\r\n';
        
        String body = header + EncodingUtil.base64Encode(fileData) + footer;
        
        req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
        req.setHeader('Content-Length', String.valueOf(body.length()));
        
        req.setBody(body);
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        
        if (res.getStatusCode() == 200) {
            // File uploaded successfully
        } else {
            // Handle error response
        }
    }
}