I am working on Zalo API to get the File token, wrote below code but the API is giving me Status 200 with response body :
{“error”:-201,”message”:”File not exist!”}
Here is my sample code:
String fileId = '069HE0000010ZurYAE'; // contentDocumentId
String 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 + 'rn' +
'Content-Disposition: form-data; name="field1"' + 'rn' +
'--' + boundary + 'rn' +
'Content-Disposition: form-data; name="file"; filename="' + fileName + '"' + 'rn' +
'Content-Type: application/octet-stream' + 'rn' + 'rn' +
fileData + 'rn' +
'--' + boundary + '--' + 'rn';
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);
And 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 in apex class it is giving me errors.
2