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 });- ✓Correct answer: Build a
FormDatafrom the form element and pass it as the body - 2Read every input manually and build a JSON string
- 3Call
form.submit()insidefetch
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.