tomas / needle

Nimble, streamable HTTP client for Node.js. With proxy, iconv, cookie, deflate & multipart support.

Home Page:https://www.npmjs.com/package/needle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multipart uploads strips date values in the request body

jcorscadden opened this issue · comments

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

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

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.