tomas / needle

Nimble, streamable HTTP client for Node.js. With proxy, iconv, cookie, deflate & multipart support.
https://www.npmjs.com/package/needle
MIT License
1.62k stars 235 forks source link

Multipart uploads strips date values in the request body #432

Open jcorscadden opened 8 months ago

jcorscadden commented 8 months ago

I am making a POST request using needle to upload a file to our API, I'm also including metadata with that file in the body of the request.

I've noticed that I can upload date objects using needle post requests only when I am not also uploading an object, if I do include an object (multipart upload) then the date values are stripped out of the request.

I believe this is happening because the body is stringified only if the upload reaches the last else statement in the big chain if if else statements in the start function in the needle code. I can get around this issue by just stringifying the date fields myself prior to passing the body to needle, but it would be nice if date fields were handled the same way whether or not you are performing a multipart upload

tomas commented 8 months ago

Hi, can you provide a small snippet so I can reproduce at my end?

jcorscadden commented 8 months ago

const endpoint =https://test-api.test.devenv/`; const data = { 'title' : 'Test Title', 'createdDate' : new Date(), file : { file : 'test/fixtures/local/neil-warnock-warnock.gif', content_type : 'image/gif' } };

const options = {
    headers: {
        'Token': 'xyz'
    },
    responseType: 'json',
    multipart: true
};

const result = await needle( 'post', endpoint, data, options );`

Have anonymised it quite a bit, but that's the gist of how it's working. If I log out the parameters I receive in the API side the createdDate isn't there. If I change it so that I do .toISOString() instead it works fine, so looks like the issue is due to the date type. If I don't provide 'multipart: true' in the options it will also work.