0

I have a follow up question for another question I asked Formdata isnt working in Apex

/tldr I'm using an api that uses formdata but now I need to add a pdf file to the formdata

So what I'm trying to do is to use a visualforce page as the file (it uses the renderAs='Pdf' tag). When looking at how to do it on postman it looks like this (even when I add my file manually).

Content-Disposition: form-data; name="Files"; filename="[PROXY]"
Content-Type: <Content-Type header here>

(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW--

And when trying to add the visualforce page to the formdata I tried

PageReference pdf = new pagereference('/apex/renderAsPdf');
        Blob file;
        try {
            file = pdf.getContentAsPDF();
            System.Debug('Successful'+file);
        } catch (Exception e) {
            file = Blob.valueOf('data');
            system.debug('Error'+ e);
        }

 body += 'name=\"Files\"; filename=\"lease.pdf\"\r\n\r\n'+EncodingUtil.base64Encode(file)+'\r\n';
 body += '------WebKitFormBoundary7MA4YWxkTrZu0gW--';

I also tried doing files by itself and tried putting the blob into a content version and adding that

ContentVersion CV = new ContentVersion();
CV.Title = 'L.pdf';
CV.PathOnClient = 'L.pdf';
CV.VersionData = file;

body += 'name=\"Files\"; filename=\"lease.pdf\"\r\n\r\n'+CV+'\r\n';
body += '------WebKitFormBoundary7MA4YWxkTrZu0gW--';

But everything gives me a 422 error::"Unsupported file(s). Supported file formats are PDF, Word, PNG, JPEG, and JPG"

2
  • We have several answers on this site to help you. I've linked one such answer. Note that the site may also be complaining because you did not specify a content-disposition.
    – sfdcfox
    Commented Mar 16, 2023 at 22:28
  • Sorry, I mistyped, lol. I meant to say, you need a Content-Type header for files. See this Q&A.
    – sfdcfox
    Commented Mar 16, 2023 at 22:43

0

Browse other questions tagged .