Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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);
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your problem is that you are sending a empty stream,

There is an "_streams" array in your form data that contains the stream of your "liscense.jpg" file, and when you POST the first request to your target host, this stream will be empty and the stream of your other requests is empty, so the file does not reach your destination.

In short, this code only send your file once in first request, and other requests do not include your file/files.

you can try this:

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');

function postRequest() {
    const data = new FormData();
    data.append('files', fs.createReadStream('./liscense.jpg'));
    const config = {
        method: 'post',
        url: 'https://domainname/scan/id',
        headers: {
            ...data.getHeaders()
        },
        data: data
    };
    return config;
}

axios
    .all([axios(postRequest()), axios(postRequest())])
    .then(
        axios.spread((res1, res2) => {
            console.log(res1.data, res2.data);
        })
    )
    .catch((error) => {
        console.log(error);
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda