I was having trouble adding Files to a formdata and my question (How to add a file to a formdata for an api in apex) was closed because there were other solutions that should work. I've tried Compose a multipart/form-data request from apex without base64 encode and How can I compose a multipart/form-data request? (though I had to edit the class a bit) I still get the 422 error.
//I used .writeFile( 'Files', 'Lease.pdf', EncodingUtil.base64Encode(file)) //From the pdf.getContentAsPdf() //then .getFormAsBlob(); req.setHeader( 'Connection', 'keep-alive' ); req.setHeader( 'Content-Length', String.valueOf(formData.size()) ); req.setHeader( 'Content-Type', HttpHexFormBuilder.GetContentType() ); req.setBodyAsBlob(formData); //And even the manual way body += 'name=\"Files\"; filename=\"Lease.pdf\"; Content-Type=\"application/pdf\"\r\n\r\n'+EncodingUtil.base64Encode(file)+'\r\n'; //Then req.setBody(body);
I even tried using the VersionData of a file that I already have
ContentVersion cv = [Select VersionData from ContentVersion where id = '068Do000002J0ArIAK']; .writeFile( 'Files', 'lease2.pdf', EncodingUtil.base64Encode(cv.VersionData))
And still nothing. It looks like it should work but I keep getting the same 422 error.