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
Jugbeer BholaJugbeer Bhola 

Multipart POST to REST

Hello, I have been searching, attempting to find a functioning example of how to POST multipart base64 attachments and associated parent documents to a third party service.  I see pieces for just attachments and pieces for documents but nothing together.  

An example would be to send an application (document) with attachments (resume, photo Id, pdf) to the 3rd party in a REST Post. 

Does anyone have a working example of this?  Don't know if I need to send the base64 attachmetns by themselves or within the body the request with the document.   
SwethaSwetha (Salesforce Developers) 
HI Jugbeer,
To send an application (document) with attachments (resume, photo ID, PDF) to a third-party service in a REST Post, you can use the multipart/form-data content type to send the base64-encoded files as part of the request body. The approach would be

>  Use Apex to convert the files to base64-encoded strings. You can use the EncodingUtil class to do this.
>Use Apex to create the multipart/form-data request body. The request body should include the document and attachment parts, each with their own content type and base64-encoded data.
>Use Apex to send the REST Post request to the third-party service. Set the content type of the request to multipart/form-data and include the request body in the request.
Example:
String boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
String header = '--' + boundary + '\r\nContent-Disposition: form-data; name="document"; filename="application.pdf"\r\nContent-Type: application/pdf\r\n\r\n';
String footer = '\r\n--' + boundary + '\r\nContent-Disposition: form-data; name="resume"; filename="resume.pdf"\r\nContent-Type: application/pdf\r\n\r\n' + resumeBase64 + '\r\n--' + boundary + '\r\nContent-Disposition: form-data; name="photo_id"; filename="photo_id.jpg"\r\nContent-Type: image/jpeg\r\n\r\n' + photoIdBase64 + '\r\n--' + boundary + '--';
String body = header + documentBase64 + footer;

Related: https://salesforce.stackexchange.com/questions/24108/post-multipart-without-base64-encoding-the-body

https://stackoverflow.com/questions/65350640/to-upload-a-file-what-are-the-pros-and-cons-of-sending-base64-in-post-body-vs-mu

https://community.smartbear.com/t5/ReadyAPI-Questions/How-to-post-a-REST-with-Json-value-and-base64-encoded-file-as-a/td-p/178203

If this information helps, please mark the answer as best. Thank you
Jugbeer BholaJugbeer Bhola
Swetha, Thank you for taking the time to answer.  What you are communicating is that the application (document) is not JSON?  I see variables in the code you posted  pertaining to base64. Did not see how you converted it to base64.   Are all objects converted to base64 and sent that way to the REST service.  No JSON at all?