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.