Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
Why isn't the POST in npm-got sending arrays properly when using form?

I wanted to recreate my python requests code in javascript for an API.

Python code:

response = requests.post("https://httpbin.org/post",data={
    "test":["hello","world"]
})

print(response.text)

which returns:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "test": [
      "hello",
      "world"
    ]
  },
  "headers": { "i removed"},
  "json": null,
  "origin": "i removed",
  "url": "https://httpbin.org/post"
}

Then when I try to use npm-got to recreate the same request this happens:

const data = await got.post('https://httpbin.org/post', {
    form : {"test":["hello","world"]}
})

console.log(data.body);

which returns:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "test": "hello,world"
  }, 
  "headers": {"i removed"}, 
  "json": null, 
  "origin": "i removed", 
  "url": "https://httpbin.org/post"
}

Why does the array turn into a string with a comma? How could I make it POST like the python request?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

have you tried the KY instead that seems to match up better

import ky from 'https://cdn.skypack.dev/ky?dts';

const formData = new FormData();
formData.append('test', 'hello');
formData.append('test', 'world');
const response = await ky.post("https://httpbin.org/anything", {body: formData}).json();
console.log(response);

See https://codepen.io/ptahume/pen/poLRJKN

I hope this is of some help

about 4 years ago · Santiago Gelvez Denunciar

0

try adding .json() to then end of the function call

got.post(...., { test:['hello','world']}).json()

objects and JSON are not the same in JS, need to convert the object, the npm-got library has a function to do that, or you can use js native JSON class: data = JSON.stringify({test: ['hello','world']});

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda