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
Adheena jacob 1Adheena jacob 1 

Rest API to upload Attachment to API by assigning to a key

Hi, I'm trying to upload file to Google cloud. I need to assign the attachment to a key "document" and then upload using REST API
This is a screenshot from postman. I need to upload file in this format using REST API.
In java you achieve this by doing something like this
.body("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"document\"; filename=\"Test.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; )
How do we do this in apex. 
Can anyone help? Thanks in advance
Adheena

Raj VakatiRaj Vakati
You could able to do it as below 
body += '--'+boundary+'\nContent-Disposition: form-data; '+
  'name="flowBody"; filename="flowFileName"\nContent-Transfer-Encoding: base64\n\n'+
  EncodingUtil.base64Encode(bodyBlob)+'\n'+

Refer this link 

https://salesforce.stackexchange.com/questions/180311/send-blob-file-via-apex-rest-callout

 
Adheena jacob 1Adheena jacob 1
Thanks Raj. I want to know how you can assign an attachment body as a value to the key. My request looks like this now

--------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; 
Content-Type:  multipart/form-data; name="document";

Blob[221218]

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="storage_url"

folder/directory/test.pdf
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="md5"

85A712140AC1170FEE4988FBD8E1F010
------WebKitFormBoundary7MA4YWxkTrZu0gW

If I add only storage_url and md5 the system says you need to provide values for all 3 fields. When I add the third key value pair, ie document and attachment body, I get 'Internal server error'. So what I need is a way to assign attach key value pair for uploading salesforce attachments and some other fields in http request body. 
I tried encoding and converting to blob like this
http://enreeco.blogspot.com/2013/01/salesforce-apex-post-mutipartform-data.html

Also tried key value pair like this
req.setBody('"key"="value", "key2" = "value2"') 

No converting and encoding like
String requestBody ='--'+boundary+ '\r\nContent-Disposition: form-data;\r\nContent-Type: multipart/form-data; name="document";\r\n\r\n'+file.body+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="storage_url"\r\n\r\nfolder/directory/Test.pdf\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="md5"\r\n\r\n85A712140AC1170FEE4988FBD8E1F010\r\n'+'--'+boundary+'--';

But this gave me the same Internal Server Error response.

Any one have any suggestions? 
Vikash Kumar 225Vikash Kumar 225
Did anybody handle large size attachment like 15 MB where JSON data exceeding 6M charater limit?
SWATI KUSHWAHASWATI KUSHWAHA
Hi Adheena jacob .....did you get the solution?