FormData: Package Form Data for HTTP Requests

The FormData API is a digital shipping container for your form's data, packaging input into a format HTTP requests understand. Use it to send forms, including files, with fetch() without manually setting headers.
Why it exists
Before FormData, developers had to manually collect form values, encode them correctly (especially for file uploads), and set the right Content-Type headers for AJAX requests. This was tedious and error-prone. FormData was created to automate this entire process, simplifying form submissions in JavaScript.
The mental model
FormData is a specialized key/value store that mirrors an HTML form. It acts as a ready-to-send package for your data, handling the complex multipart/form-data encoding for you, which is crucial for file uploads. You can give it a <form> element and it will grab all the inputs, or you can build it piece by piece using its methods.
How it works
You create a new FormData object, optionally passing an HTML <form> element to its constructor to pre-populate it with that form's data. You can then manipulate the data using methods like append(key, value), set(key, value), and delete(key). When you pass this FormData object as the body of a fetch() request, the browser automatically sets the correct Content-Type: multipart/form-data header and serializes the data for you.
When to use it
Use FormData whenever you need to submit form data via JavaScript, especially when the form includes file uploads (<input type="file">). It's also useful for programmatically constructing data that mimics a form submission without an actual <form> element on the page. It works seamlessly with fetch(), XMLHttpRequest, and navigator.sendBeacon().
When not to use it
For simple key/value data without files, sending a plain JSON object with Content-Type: application/json can be simpler and is more common for modern APIs. If you only need to create a URL query string for a GET request, using URLSearchParams directly might be more straightforward, although FormData can be passed to its constructor.
One canonical example
To send a form with an ID user-profile-form that includes a file input, you can write: const myForm = document.getElementById('user-profile-form'); const formData = new FormData(myForm); fetch('/api/profile', { method: 'POST', body: formData });. The browser handles all the encoding and header details.
Interview question
What is the main advantage of using the FormData API when submitting an HTML form via JavaScript?
- a.It allows form submissions to occur without requiring a full page reload.
- b.It provides built-in client-side validation for all form input fields.
- c.It ensures that all form data is securely encrypted before being sent to the server.
- d.It automatically handles the correct encoding and Content-Type header for complex data, including file uploads.Correct
Why? this is the answer
The FormData API was created to automate the tedious and error-prone process of collecting, encoding (especially multipart/form-data for files), and setting the correct Content-Type headers for AJAX form submissions. While it is often used with asynchronous requests that prevent page reloads, FormData itself doesn't enable the no-reload aspect; it simplifies the data preparation for such requests.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #web api
- #javascript
- #forms
- #http
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles