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

174
Visualizações
How to make three POST requests using node-fetch

I am making POST requests to an API and I want to make three requests as efficient and with less code as possible. Currently my code works, but it sends only the first object, instead of all three, what is the most efficient way to make three API calls? Here is my attempt so far:

import fetch from 'node-fetch';
import xlsx from 'xlsx';
const baseUrl = "";
const apiToken = "";
const wb = xlsx.readFile('users.xlsx');
const ws = wb.Sheets['users'];
const data = xlsx.utils.sheet_to_json(ws);

let [obj1, obj2, obj3] = data

const optionsForFirstUser = {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiToken}`,
    "gtmhub-accountid": accountId,
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  },
 

  body: JSON.stringify(
    obj1
  ),
};

const optionsForSecondUser = {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiToken}`,
    "gtmhub-accountid": accountId,
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  },
 

  body: JSON.stringify(
    obj1
  ),
};

const optionsForThirdUser = {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiToken}`,
    "gtmhub-accountid": accountId,
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  },
 

  body: JSON.stringify(
    obj1
  ),
};


const createUser = (url, settings) => {
  return fetch(`${url}/users`, settings)
    .then((response) => response.text())
    .then((data) => console.log(data))
    .catch((error) => {
      console.log(error.message);
    });
};

createUser(baseUrl, optionsForFirstUser, optionsForSecondUser, optionsForThirdUser);

I can't use an array, because the API doesn't allow it. It needs to be just JSON objects.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is a working example that sends multiple POST requests to a test Postman API:

const fetch = require("node-fetch");

const baseUrl = "https://postman-echo.com/post";

const obj1 = { id: 1, name: "one" };
const obj2 = { id: 2, name: "two" };
const obj3 = { id: 3, name: "three" };

const options1 = {
  method: "POST",
  headers: {
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  },
  body: JSON.stringify(obj1),
};

const options2 = {
  method: "POST",
  headers: {
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  },
  body: JSON.stringify(obj2),
};

const options3 = {
  method: "POST",
  headers: {
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  },
  body: JSON.stringify(obj3),
};

const createUser = (url, options) => {
  return options.map((option) => fetch(url, option));
};

(async () => {
  const promises = createUser(baseUrl, [options1, options2, options3]);

  try {
    const rc = await Promise.all(promises);
    console.log(rc);
  } catch (e) {
    console.log("Caught error: ", e);
  }
})();
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