Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3643

Issue with form-data while using in Apex REST API callout

$
0
0

I am working on an API to get the File token, wrote the below code but the API is giving me Status 200 with the response body :

{"error":-201,"message":"File not exist!"}

Here is my curl request:

curl --location 'openapi.zalo.me/v2.0/oa/upload/file' \ --header 'access_token: <access_token_here>' \ --header 'Accept: application/json' \ --form 'file=@"/C:/Users/Desktop/Sample Files/file-sample_150kB.pdf"'

It is working fine with the postman, but when trying to use it in apex class, it gives me errors.

Apex code:

String fileId = '069HE0000010ZurYAE'; // contentDocumentIdString fileName = 'file-sample_150kB.pdf';ContentVersion contentVersion = [SELECT Id, ContentSize, Title, ContentDocumentId, VersionData FROM ContentVersion WHERE ContentDocumentId = :fileId LIMIT 1];Blob fileData = contentVersion.VersionData;String boundary = '----MyBoundary';String requestBody = '--'+ boundary +'\r\n'+'Content-Disposition: form-data; name="field1"'+'\r\n'+'--'+ boundary +'\r\n'+'Content-Disposition: form-data; name="file"; filename="'+ fileName +'"'+'\r\n'+'Content-Type: application/octet-stream'+'\r\n'+'\r\n'+    fileData +'\r\n'+'--'+ boundary +'--'+'\r\n';HttpRequest req = new HttpRequest();req.setMethod('POST');req.setEndpoint('https://openapi.zalo.me/v2.0/oa/upload/file');req.setHeader('Content-Type', 'multipart/form-data; boundary='+ boundary);req.setHeader('access_token', <access_token_here>);req.setBody(requestBody);Http h = new Http();HttpResponse response = h.send(req);

Viewing all articles
Browse latest Browse all 3643

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>