Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
¿Por qué el POST en npm-got no envía matrices correctamente cuando se usa el formulario?

Quería recrear mi código de solicitudes de python en javascript para una API.

código pitón:

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

que devuelve:

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

Luego, cuando trato de usar npm-got para recrear la misma solicitud, sucede lo siguiente:

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

que devuelve:

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

¿Por qué la matriz se convierte en una cadena con una coma? ¿Cómo podría hacer que sea POST como la solicitud de python?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

¿Has probado el KY que parece coincidir mejor?

 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);

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

Espero que esto sea de ayuda

about 4 years ago · Santiago Gelvez Report

0

intente agregar .json () al final de la llamada a la función

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

los objetos y JSON no son lo mismo en JS, es necesario convertir el objeto, la biblioteca npm-got tiene una función para hacerlo, o puede usar la clase JSON nativa de js: data = JSON.stringify({test: ['hello','world']});

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!