You need to sign in to do that
Don't have an account?

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
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
Refer this link
https://salesforce.stackexchange.com/questions/180311/send-blob-file-via-apex-rest-callout
--------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?