How to POST with multipart/form-data header and FormData using fetch How to POST with multipart/form-data header and FormData using fetch curl curl

How to POST with multipart/form-data header and FormData using fetch


If you open up your network inspector, run this code snippet, and submit the form you should see that the Content-Length is set correctly:

const foo = document.getElementById('foo')foo.addEventListener('submit', (e) => {  e.preventDefault()  const formData = new FormData(foo)  formData.append('userId', 123)  fetch('//example.com', {    method: 'POST',    body: formData  })})
<form id="foo">  <input id="file" type="file" name="file"/><br><br>  <button type="submit">Submit</button></form>