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

126
Views
Post Request with forms (and JavaScript fetch())

How can i send a form parameter in a post (JavaScript fetch()) request? e.g. curl --form "avatar=@me.jpg" "https://example.com/api/v4/endpoint"

I tried the folloing code:

const form = new FormData()
form.append("foo":"bar")
fetch( "https://someapi.org/api", {
    method: 'POST',
    headers:form
} )
    .then( response => response.json() )
    .then( response => {
        console.log(response)
    } );
}

which doesn't work for me.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The FormData should be supplied as the body property of the RequestInit, like this:

Make sure you use the correct Content-Type header. You can read about Using FormData Objects on MDN.

TS Playground

so-70865955.ts:

async function example () {
  const form = new FormData();
  form.append("foo", "bar");

  const apiAddress = "https://someapi.org/api";

  const init: RequestInit = {
    method: 'POST',
    headers: new Headers([['content-type', 'application/x-www-form-urlencoded']]),
    body: form,
  };

  const response = await fetch(apiAddress, init);
  const data = await response.json();
  console.log(data);
}

// Invoke it
example();

In your console:

deno run --allow-net so-70865955.ts
about 4 years ago · Juan Pablo Isaza 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!