2

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

  • I think you’re messing this up by explicitly specifying `Content-Type: multipart/form-data` ... this header needs to include the boundary used to separate the different form parameters send from one another - but because you are setting this header yourself, without any boundary, this value is missing on the receiving end. Try not specifying that header at all (I’d expect node to be clever enough to set this header itself automatically, and include the used boundary as well.) – CBroe Jan 16 '18 at 14:01
  • Thanks a lot for your answer. I don't think that node set any header without your asking it to do so. I tryed to do as you said and remove the header but it didn't change the result. – Antoine Drouhin Jan 16 '18 at 15:53
  • Well then you’d have to check and see if you can extract the boundary value used from the body (`uploadFileForm`) somehow, so that you can add it to the header yourself. – CBroe Jan 16 '18 at 15:58
  • @CBroe I just updated the code as you suggested. It doesn't work, thanks again for your help – Antoine Drouhin Jan 17 '18 at 10:17
  • 1
    Well then I’d suggest that you send this to a script of your own first, where you log the headers and request body - then you can compare what the difference is between your cURL request, and your node-fetch attempt ... – CBroe Jan 17 '18 at 10:19
  • Thanks to your help, I was able to understand what went wrong. Thanks a lot ! – Antoine Drouhin Jan 17 '18 at 13:49
  • Great! If that resulted in any knowledge that might be worthwhile for future readers, then you could write a short (self-)answer perhaps …? – CBroe Jan 17 '18 at 13:54

1 Answers1

0

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.