Search code examples
javascriptnode.jscurlveracodenode-fetch

How to translate CURL -F @filename to nodejs


I have to translate a working curl request in nodejs javascript. I did succeed with previous similar queries that didn't implied file transfer.

curl --compressed -u $username:$password https://analysiscenter.veracode.com/api/5.0/uploadfile.do -F "app_id=1" -F "file=@package.zip"

My current, non working, code :

const fetch = require("node-fetch")

const b = new Buffer(myUsername + ":" + myPassword)
const s = b.toString('base64')

let uploadFileForm = new FormData()
uploadFileForm.append('app_id', "1")
uploadFileForm.append('file', fs.createReadStream('package.zip'))

header = {
    "Authorization": `Basic ${s}`,
    "Content-Type": 'multipart/form-data; boundary=' + form.getBoundary()
}

fetch("https://analysiscenter.veracode.com/api/4.0/uploadfile.do",
    {
        method: 'POST',
        header,
        body: uploadFileForm
    })
    .then(res => {
        return res.text()
    })
    .then(body => {
        console.log(body)
    })

I use the same exact authentication process to call the api with other requests.

UPDATE

As suggested I changed my code to set the boundaries in the header. Now header look like this :

{ Authorization: 'Basic anVsaWVuLmZleWV1eEAzZHMuY29tOjVZQzA1RlVlTmpBNWdGZF5EMVRYMEQyJFNrTEdNTA==',
  'content-type': 'multipart/form-data; boundary=--------------------------439864217372737605994368' }

It still doesn't work


Solution

  • Resolution :

    The issue was a small js error as node-fetch was expecting headers when instead I provided header.

    However this was hard to see due to lack of node "Network" debugging tool.

    I used another server to check the final request headers and understand what went wrong.

    Thanks again @CBroe for providing methodology tips on how to solve that kind of problems.