0
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?

2
  • 1
    Depends entirely upon what the server expects/cares about. urlencoded form data seems most appriopriate here based on the request body I think. Commented Oct 14, 2022 at 22:46
  • 2
    The body format name=value&name=value is Url-encoded. Commented Oct 14, 2022 at 22:50

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! As a newbie, it's confusing sometimes. I appreciate your help.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.