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

229
Views
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 answers
Answer question

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 Report

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 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!