POST json file using request module

691 views Asked by At

I am planning to translate the curl API call below to node.js though I am still getting an error when doing a POST.

curl  -X POST --user user1@customer1:secret http://demo.test.com/controller/actions/38 -F [email protected]

Below is initial code using the request - npm module, but the API call still fails.

var requestdata = fs.readFileSync('./ExportActions.json').toString();

var request = require('request');
request.post({
     url: 'https://demo.test.com/controller/actions/38',
     auth: {
        'user': 'user1@customer1',
        'pass': 'secret'
    },
    body: requestdata
}, function(error, response, body){
   console.log(body);
});

I am getting the error below every time I run the script:

Could not import Actions: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

Are there any workarounds when posting a JSON file using the npm request module?

Thanks!

2

There are 2 answers

0
Thomas Shelby On

You can use Postman client to check this api instead of curl.

2
Mark On

You are trying to upload to file by posting it to the body. It looks like the server wants a multi-part upload instead. This might work better

var req = require('request');
request.post({
   url: 'https://demo.test.com/controller/actions/38',
   auth: {
      'user': 'user1@customer1',
      'pass': 'secret'
  },
}, function(error, response, body){
     console.log(body);
});

var form = req.form()
form.append('file', fs.readFileSync('./ExportActions.json'));