2

I'm using a Node.js app as proxy for a webservice. I'm able to transfer simple application/json request with body-parser but it cant handle multipart/form-data.

To do so, I'm trying to use the request module but I have an error when I attach files to the request :

Error: write after end
    at ClientRequest.OutgoingMessage.write (_http_outgoing.js:413:15)
    at Request.write(./node_modules/request/request.js:1362:25)

Here's the code that triggers this error :

var form_data = {};

            for (var key in req.files){
                form_data[req.files[key].fieldname] = fs.createReadStream(req.files[key].path);
                log.debug("File :", req.files[key].fieldname)
            }

            var multipart_request = request_module({
                url: 'http://example.com',
                method: ‘POST’,
                headers: req.headers,
                body: JSON.stringify({<some content here>}),
                formData: form_data
            }, function(error, response, body){

                if(error){
                    log.debug(error);
                    res.status(500).send('{"message": "Server error.}');
                }
                else{
                    res.status(response.statusCode).send(body);
                }

            });

Is an other solution to do that ?

2
  • 1
    I don't think you can have both body and formData. Did you try leaving off body? Commented Aug 11, 2015 at 13:16
  • That was the error, i deleted the body and It works now, thanks ! Commented Aug 11, 2015 at 13:29

1 Answer 1

4

If you supply form or formData, you cannot also have body in your request options.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.