close

How do I post form data with fetch api?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I post form data with fetch api? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I post form data with fetch api?

  1. How do I post form data with fetch api?

    do I post form data with fetch api Use FormData and fetch to grab and send data
    fetch(form.action, {method:'post', body: new FormData(form)});

  2. How do I post form data with fetch api?

    do I post form data with fetch api Use FormData and fetch to grab and send data
    fetch(form.action, {method:'post', body: new FormData(form)});

Method 1

Use FormData and fetch to grab and send data

fetch(form.action, {method:'post', body: new FormData(form)});

Method 2

With fetch api it turned out that you do NOT have to include headers “Content-type”: “multipart/form-data”.

So the following works:

let formData = new FormData()
formData.append("nameField", fileToSend)

fetch(yourUrlToPost, {
   method: "POST",
   body: formData
})

Note that with axios I had to use the content-type.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment