Categories
JavaScript Answers

How to make a post request with form data with JavaScript and Axios?

Spread the love

Sometimes, we want to make a post request with form data with JavaScript and Axios.

In this article, we’ll look at how to make a post request with form data with JavaScript and Axios.

How to make a post request with form data with JavaScript and Axios?

To make a post request with form data with JavaScript and Axios, we can call axios.post with the form data object as the 2nd argument.

For instance, we write:

const formData = new FormData()
formData.append('title', 'foo')
formData.append('description', 'bar')

axios.post('https://jsonplaceholder.typicode.com/posts', formData)

We create a new FormData instance and add some key-value pairs to it with append.

Then we call axios.post with URL to make the request to and the formData object respectively.

We can see form data sent from Chrome’s Network tab’s Payload tab of the request to https://jsonplaceholder.typicode.com/posts.

Conclusion

To make a post request with form data with JavaScript and Axios, we can call axios.post with the form data object as the 2nd argument.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *