Jojo
Jojo

Reputation: 81

What should be the Content-type header in this fetch API Javascript Code?

fetch(url, {
  method: 'post',
  headers: {
    "Content-type":
  },
  body: 'bar= foo& lorem=ipsum'

})

Should it be?

A) application/x-www-form-urlencoded; charset=UTF-8

B) text/html; charset=utf-8

C) application/json; charset=UTF-8

D) Content-Type: multipart/form-data; boundry=something

I've just started to learn JavaScript and APis. While looking at MDN docs I found there are different options to put in the headers. Just confused about which one to use?

Upvotes: 0

Views: 1216

Answers (2)

mes shahdat
mes shahdat

Reputation: 182

if you are sending url encoded data (ex: key=value&key=value) then Content-Type will be:

  • A) application/x-www-form-urlencoded; charset=UTF-8

if you are sending html data (ex: <h1>hello</h1>) then Content-Type will be:

  • B) text/html; charset=utf-8

if you are sending json data (ex: {"name" : "anyName"}) then Content-Type will be:

  • C) application/json; charset=UTF-8

if you are sending form data + files (ex: new FormData(document.getElementById('form1'))) then you don't need to set Content-Type manually that is:

  • D) multipart/form-data; boundry=something

Upvotes: 1

Barmar
Barmar

Reputation: 780974

A sequence of key=value parameters separated by & is the URL-encoded format of parameters. So A is the correct answer.

Upvotes: 2

Related Questions