JavaScript · Middle · Question 64 of 75

What is the easiest way to send all fields of a <form> with fetch?

const data = new FormData(form);
await fetch("/api/profile", { method: "POST", body: data });

Explanation

FormData includes files and sets the correct multipart/form-data content type with boundary. Don't set Content-Type yourself, or the boundary will be missing.

Further reading

Problem with a question

JavaScript · Middle · question 64

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz