I am trying to send a PDF file through multipart/form-data to Box.com's API. The PDF data is encoded in Base64. Since my module is being run through the GraalVM, which is setup in a way that node's built-in modules are blocked, I have to create the form-data payload from scratch.
The main issue that I am having is that the PDF keeps being stored as text and not being decoded back into a binary file.
Here's my code:
const testFile = file.load(321321654)
const boundary = 'ikjbhciuydgwyigdchbwikcbjlikwudhqcviudgwvcihblh'
const header = {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
'Authorization': 'Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX'
}
const body = []
body.push('--' + boundary)
body.push('Content-Disposition: form-data; name="attributes"');
body.push('')
body.push(JSON.stringify({name: testFile.name, parent: { id: 12216548 }}))
body.push('--' + boundary)
body.push('Content-Disposition: form-data; name="testfile"' + '; filename="' + testFile.name + '"')
body.push('content-type: application/pdf;charset=UTF-8')
body.push('Content-Transfer-Encoding: BASE64')
body.push('')
body.push(testFile.getContents())
body.push('--' + boundary + '--')
body.push('')
const result = https.post({
url: 'https://upload.box.com/api/2.0/files/content',
headers: header,
body: body.join('\r\n')
})
The results of the body.join is:
--ikjbhciuydgwyigdchbwikcbjlikwudhqcviudgwvcihblh
Content-Disposition: form-data; name="attributes"
{\"name\":\"Get Started with Box.pdf\",\"parent\":{\"id\":207753393458}}
--ikjbhciuydgwyigdchbwikcbjlikwudhqcviudgwvcihblh
Content-Disposition: form-data; name="file"; filename="Get Started with Box.pdf"
content-type: application/pdf;charset=UTF-8
Content-Transfer-Encoding: BASE64
JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDw (actual payload string shortened)
--ikjbhciuydgwyigdchbwikcbjlikwudhqcviudgwvcihblh--
Unfortunately, the file is loaded into box, but to decoded back to a PDF binary. It's just a text file of the Base64 string.
I have tried this with a local express server that I created and have a similar issue when using the Multer package to handle a multipart/form-data upload. Oddly, when I switch to the Formidable package it works just fine with my above code.
I've looked through the form-data specification and the payload I am building looks correct to me. The box.com forums have others asking a similar question to mine, but no one has provided an answer to any of those. A google search and stack overflow search hasn't turned up a similar question to mine.
Does anyone have any suggestions as to what may be happening?