+ 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

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'+

Refer this link 


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

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


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

Content-Disposition: form-data; name="md5"


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

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?
Hi Adheena jacob .....did you get the solution?