Add support for multipart/form-data in request node

Hi all, I've been working on adding support for multipart/form-data type data in POST requests with the stanard http request node.

Currently, I have it working with this type of message going into the http request node.

var fileData = msg.payload;
msg.headers = {'Content-Type': 'multipart/form-data'};

msg.payload = {
    'image' : {
        'value': fileData,
        'options': {
            'filename': 'image.png'
        }
    }
};

return msg;

Any feedback on this? If it looks good, I can go ahead and open a pull request on gitHub or work on some supporting documentation.

1 Like

Hi @kastentx

that looks good. Presumably the key thing here is for the content-type header to be set to multipart/form-data and that msg.payload is then an object of key/value pairs to use as the form data.

It is possible that flows will exist that set the content-type header to multipart/form-data and that also construct the required payload from scratch, so msg.payload will be a Buffer or String. In those cases, we should make sure they pass through as-is so those flows continue to work.

Thank you @knolleary!

I've gone ahead and added another conditional check to the relevant section in 21-httprequest.js. I thought about checking for something NOT typeof String or Buffer, but thought it might also be a good place to just check for an Object. This should allow strings, buffers, and numbers to pass through unaffected by the change.

Here's that part of code with the check:

if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") {
                if (opts.headers['content-type'] == 'multipart/form-data' && typeof payload === "object") {
                    opts.formData = msg.payload;
                } else {
        ...
        ...