'multiple asynchronous post request with files in nodejs

I've tried to send a bunch of post requests with file upload in nodejs. using axios.post, I could make a single request easily. But I got an error when trying send multiple asynchronous requests.

Based on the axios document, it uses axios.all([ axios.get(), axios.get(), ...]) to make async requests at time.

If I sent my code, the error says:

"Error: Request failed with status code 500 ~ "

. This error is the same when I send a request without file upload. So I guess my code doesn't attach a file when I send async request.

Please advise me what I am missing.

My code is below:

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
data.append('files', fs.createReadStream('./liscense.jpg'));

var config = {
  method: 'post',
  url: 'https://domainname/scan/id',
  headers: { 
    ...data.getHeaders()
  },
  data : data
};

axios
.all([axios(config), axios(config)])
.then(
    axios.spread((res1, res2) => {
        console.log(res1.data, res2.data);
    })
)
.catch((error) => {
  console.log(error);
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source