user3700562 user3700562 - 5 months ago 28
HTTP Question

Get HTTP Body of Form in JavaScript

Is it possible in Javascript to get the contents of the HTTP Body of the resulting HTTP request that would be sent when a Form is submitted?

Say I have a Form of type 'POST' with a file input and some other input fields and an enctype attribute of 'multipart/form-data'. I would like to get the verbatim HTTP Body and possibly HTTP Headers of the request the browser would generate if I submitted the form (without actually submitting it).


You can use Response, Response.body.getReader() which is a ReadableStream to read the contents of FormData object. Use TextDecoder to convert Uint8Array to readable text. Note, you cannot read stream and headers from the same Response object. Though you can create a new Response object from same input data and the read Headers

var form = document.querySelector("form");
form.onsubmit = (e) => {
  var formData = new FormData();
  var input ="input");
  formData.append("file", input.files[0], input.files[0].name);

  var response = new Response(formData);
  var stream = response.body;
  var reader = stream.getReader();
  var decoder = new TextDecoder();
    .then(function processData(result) {
      if (result.done) {
        console.log("stream done");
      var data = decoder.decode(result.value);

    .catch(function(err) {
      console.log("catch stream cancellation:", err);

  reader.closed.then(function() {
    console.log("stream closed");
<form method="POST" enctype="multipart/form-data">
  <input name="file" type="file" />
  <input type="submit">