1

I am trying to send a file via the request npm library, but I keep getting the write after end error. What's wrong? How can I fix this?

This is the way I'm doing this:

    return new Promise((resolve, reject) => {
        const callback = (error, response, body) => {
            if (!error && response.statusCode < 400) {
                resolve(body);
            }
            else {
                reject(error); // Write after end
            }
        };
        request({
            url: "https://api.url.com/v2/",
            method: "POST",
            formData: fs.createReadStream("file"),
        }, callback);
    });
user99999
  • 1,994
  • 5
  • 24
  • 45

1 Answers1

0

I think the way to send data is to append the form data to the request:

var req = request.post(url, callback);

var form = req.form();
form.append('file', fs.createReadStream(filepath));
driedel
  • 178
  • 9
  • possible duplicate: http://stackoverflow.com/questions/25344879/uploading-file-using-post-request-in-node-js – driedel May 16 '17 at 12:21
  • looks like the `Promise` is your problem. Have a look @ http://stackoverflow.com/questions/37813500/how-do-i-send-a-form-with-a-request-promise – driedel May 16 '17 at 14:31