I need help uploading file from salesforce to another application.
i tested this on postman and it works but I keep getting this error when I try it in salesforce
Transfer Enconding should be set to chunked 406
I dont understand what to do
ContentVersion contentVersion = [SELECT VersionData, Title FROM ContentVersion LIMIT 1];
String fileName = contentVersion.Title;
String fileBody = EncodingUtil.base64Encode(contentVersion.VersionData);
String dataString = '{
"Tuples":[{"Name":"Keywords","Value":"File Added via Postman"},{"Name":"ContractFile","Value":null},
{"Name":"FileType","Value":".pdf"},
{"Name":"ContractID","Value":2},
{"Name":"FileName","Value":"SomeTestDoc.pdf"}]}';
String boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
system.debug(EncodingUtil.base64Encode(Blob.valueOf('test')));
// Create the body of the request as a String
String body = '--' + boundary + 'rn' +
'Content-Disposition: form-data; name="attachment"; filename="' + fileName + '"rnrn' +
'Content-Type: text/plainrnrn' +
fileBody + 'rn' +
'--' + boundary + 'rn' +
'Content-Disposition: form-data; name="data"rnrn' +
dataString + 'rn' +
'--' + boundary + '--rn';
// Prepare the HTTP request
HttpRequest req = new HttpRequest();
req.setEndpoint('<<baseURL>>/API2/CSSAPI/V2/Files/ContractFiles/Upload');
req.setMethod('POST');
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
req.setHeader('Transfer-Encoding', 'chunked');
req.setHeader('Authorization', 'Bearer <<Authcode>>');
req.setBody(body);
system.debug(body);
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() != 200) {
System.debug('Failed to send file: ' + res.getStatus() + ' ' + res.getStatusCode() + ' ' + res.getBody());
} else {
System.debug('File sent successfully: ' + res.getBody());
}