Categories
JavaScript Answers

How to upload file using POST request in Node.js?

Spread the love

To upload file using POST request in Node.js, we call the request function.

For instance, w write

request(
  {
    url: "http://example.com",
    method: "POST",
    formData: {
      regularField: "someValue",
      regularFile: someFileStream,
      customBufferFile: {
        value: fileBufferData,
        options: {
          filename: "myfile.bin",
        },
      },
    },
  },
  handleResponse
);

to call request to make a post request to http://example.com

We set formData to an object with the key-value pairs for the form data object.

The file is set as the value of regularFile.

We set it to a stream.

handleResponse is a function that’s called when a response is received.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *