0
String body = '' + documentList[0].VersionData+ '';
system.debug('body---'+body);
HttpRequest req = new HttpRequest();
req.setBody(body);
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);       
req.setHeader('Authorization', 'Bearer ' + accessToken);
req.setMethod('POST');
req.setEndpoint(uploadEndPointURL);
Http ht = new Http();
HTTPResponse res = ht.send(req); 

How can I upload png file. Tried above code but getting error. Thanks

0

1 Answer 1

1

It is possible to use setBodyAsBlob method instead of setBody

Sets the contents of the body for this request using a Blob.

VersionData is already a blob type value.

HttpRequest req = new HttpRequest();
req.setBodyAsBlob(documentList[0].VersionData);
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);       
req.setHeader('Authorization', 'Bearer ' + accessToken);
req.setMethod('POST');
req.setEndpoint(uploadEndPointURL);
Http ht = new Http();
HTTPResponse res = ht.send(req);

Verify documentation of the endpoint API how exactly a file should be sent.

3
  • 1
    Yes, except... this is multipart form. Additional work is necessary to make this work.
    – sfdcfox
    Commented Aug 8, 2022 at 11:41
  • @Oleksandr Berehovskyi @ sfdcfox, Thanks for answering. After using 'setBodyAsBlob' I am getting 'API upload did not contain a file part' error. Basically I wanted to know is there any way by which I can upload any file type like .csv, .png, .txt etc. Thanks
    – user106835
    Commented Aug 8, 2022 at 11:51
  • @user106835 I linked a Q&A that demonstrates how to deal with multipart/form-data. Please read that Q&A again.
    – sfdcfox
    Commented Aug 8, 2022 at 14:13

Not the answer you're looking for? Browse other questions tagged .