Puppeteer - POST request using form-data and node-fetch











up vote
0
down vote

favorite












I'm trying to make a POST request to upload a binary to a server and the form data is not being sent correctly (or at all).



My current integration of node-fetch with puppeteer works fine for POST or GET requests with {"Content-Type": "application/json"}.



Here is my code:



const FormData = require("form-data");
const fetch = require("node-fetch");
const fs = require("fs");

upload(pkgUrl) {
return new Promise(async (resolve, reject) => {

const token = ...
const url = ...

const stats = fs.statSync(pkgUrl);
const fileSizeInBytes = stats.size;
console.log("fileSizeInBytes " + fileSizeInBytes); // Prints a valid size

const form = new FormData();
form.append("version", "1.0");
form.append("appFile", fs.createReadStream(pkgUrl));
form.append("minFirmwareVersion", 8164134);
form.append("newPackage", true);

let headers = form.getHeaders();
headers["csrf-token"] = token;

const response = await this._makeHTTPRequest(url, "POST", headers, form);

(...)
});
}

_makeHTTPRequest(url, method = "GET", headers = null, body = null, parse = true) {
return new Promise(async (resolve, reject) => {
try {
const response = await this._page.evaluate(async (url, method, headers, body, parse) => {
let request = {
method: method,
credentials: "include"
};

if (headers) {
request["headers"] = headers;
}

if (body) {
request["body"] = body;
}

// Make request
const fetchResponse = await fetch(url, request);

// Return parsed (object) or plain-text response
return parse ? await fetchResponse.json() : await fetchResponse.text();
}, url, method, headers, body, parse);

resolve(response);
} catch (error) {
reject(error);
}
});
}


This is how the form data should be sent:
enter image description here



And this is how is being sent:
enter image description here



Also, the content-length being sent is only 15 bytes and it should be around 2mb.



Am I using form-data and node-fetch correctly?



Thanks!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm trying to make a POST request to upload a binary to a server and the form data is not being sent correctly (or at all).



    My current integration of node-fetch with puppeteer works fine for POST or GET requests with {"Content-Type": "application/json"}.



    Here is my code:



    const FormData = require("form-data");
    const fetch = require("node-fetch");
    const fs = require("fs");

    upload(pkgUrl) {
    return new Promise(async (resolve, reject) => {

    const token = ...
    const url = ...

    const stats = fs.statSync(pkgUrl);
    const fileSizeInBytes = stats.size;
    console.log("fileSizeInBytes " + fileSizeInBytes); // Prints a valid size

    const form = new FormData();
    form.append("version", "1.0");
    form.append("appFile", fs.createReadStream(pkgUrl));
    form.append("minFirmwareVersion", 8164134);
    form.append("newPackage", true);

    let headers = form.getHeaders();
    headers["csrf-token"] = token;

    const response = await this._makeHTTPRequest(url, "POST", headers, form);

    (...)
    });
    }

    _makeHTTPRequest(url, method = "GET", headers = null, body = null, parse = true) {
    return new Promise(async (resolve, reject) => {
    try {
    const response = await this._page.evaluate(async (url, method, headers, body, parse) => {
    let request = {
    method: method,
    credentials: "include"
    };

    if (headers) {
    request["headers"] = headers;
    }

    if (body) {
    request["body"] = body;
    }

    // Make request
    const fetchResponse = await fetch(url, request);

    // Return parsed (object) or plain-text response
    return parse ? await fetchResponse.json() : await fetchResponse.text();
    }, url, method, headers, body, parse);

    resolve(response);
    } catch (error) {
    reject(error);
    }
    });
    }


    This is how the form data should be sent:
    enter image description here



    And this is how is being sent:
    enter image description here



    Also, the content-length being sent is only 15 bytes and it should be around 2mb.



    Am I using form-data and node-fetch correctly?



    Thanks!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to make a POST request to upload a binary to a server and the form data is not being sent correctly (or at all).



      My current integration of node-fetch with puppeteer works fine for POST or GET requests with {"Content-Type": "application/json"}.



      Here is my code:



      const FormData = require("form-data");
      const fetch = require("node-fetch");
      const fs = require("fs");

      upload(pkgUrl) {
      return new Promise(async (resolve, reject) => {

      const token = ...
      const url = ...

      const stats = fs.statSync(pkgUrl);
      const fileSizeInBytes = stats.size;
      console.log("fileSizeInBytes " + fileSizeInBytes); // Prints a valid size

      const form = new FormData();
      form.append("version", "1.0");
      form.append("appFile", fs.createReadStream(pkgUrl));
      form.append("minFirmwareVersion", 8164134);
      form.append("newPackage", true);

      let headers = form.getHeaders();
      headers["csrf-token"] = token;

      const response = await this._makeHTTPRequest(url, "POST", headers, form);

      (...)
      });
      }

      _makeHTTPRequest(url, method = "GET", headers = null, body = null, parse = true) {
      return new Promise(async (resolve, reject) => {
      try {
      const response = await this._page.evaluate(async (url, method, headers, body, parse) => {
      let request = {
      method: method,
      credentials: "include"
      };

      if (headers) {
      request["headers"] = headers;
      }

      if (body) {
      request["body"] = body;
      }

      // Make request
      const fetchResponse = await fetch(url, request);

      // Return parsed (object) or plain-text response
      return parse ? await fetchResponse.json() : await fetchResponse.text();
      }, url, method, headers, body, parse);

      resolve(response);
      } catch (error) {
      reject(error);
      }
      });
      }


      This is how the form data should be sent:
      enter image description here



      And this is how is being sent:
      enter image description here



      Also, the content-length being sent is only 15 bytes and it should be around 2mb.



      Am I using form-data and node-fetch correctly?



      Thanks!










      share|improve this question















      I'm trying to make a POST request to upload a binary to a server and the form data is not being sent correctly (or at all).



      My current integration of node-fetch with puppeteer works fine for POST or GET requests with {"Content-Type": "application/json"}.



      Here is my code:



      const FormData = require("form-data");
      const fetch = require("node-fetch");
      const fs = require("fs");

      upload(pkgUrl) {
      return new Promise(async (resolve, reject) => {

      const token = ...
      const url = ...

      const stats = fs.statSync(pkgUrl);
      const fileSizeInBytes = stats.size;
      console.log("fileSizeInBytes " + fileSizeInBytes); // Prints a valid size

      const form = new FormData();
      form.append("version", "1.0");
      form.append("appFile", fs.createReadStream(pkgUrl));
      form.append("minFirmwareVersion", 8164134);
      form.append("newPackage", true);

      let headers = form.getHeaders();
      headers["csrf-token"] = token;

      const response = await this._makeHTTPRequest(url, "POST", headers, form);

      (...)
      });
      }

      _makeHTTPRequest(url, method = "GET", headers = null, body = null, parse = true) {
      return new Promise(async (resolve, reject) => {
      try {
      const response = await this._page.evaluate(async (url, method, headers, body, parse) => {
      let request = {
      method: method,
      credentials: "include"
      };

      if (headers) {
      request["headers"] = headers;
      }

      if (body) {
      request["body"] = body;
      }

      // Make request
      const fetchResponse = await fetch(url, request);

      // Return parsed (object) or plain-text response
      return parse ? await fetchResponse.json() : await fetchResponse.text();
      }, url, method, headers, body, parse);

      resolve(response);
      } catch (error) {
      reject(error);
      }
      });
      }


      This is how the form data should be sent:
      enter image description here



      And this is how is being sent:
      enter image description here



      Also, the content-length being sent is only 15 bytes and it should be around 2mb.



      Am I using form-data and node-fetch correctly?



      Thanks!







      node.js puppeteer form-data node-fetch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 4:06

























      asked Nov 11 at 3:43









      Alejandro Cotilla

      666818




      666818





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245651%2fpuppeteer-post-request-using-form-data-and-node-fetch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245651%2fpuppeteer-post-request-using-form-data-and-node-fetch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ