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.63k stars 236 forks source link

Multipart filename parameter is always URL encoded #310

Open alexburley opened 4 years ago

alexburley commented 4 years ago

Hello again!

I notice that all multipart request filename properties are automatically URL encoded and theres no way to configure this. I understand that this correlates to official spec https://tools.ietf.org/html/rfc7578 but it would be useful to be able to override this and allow the raw filename to be sent in UTF-8 encoding.

https://github.com/tomas/needle/blob/master/lib/multipart.js#L46 - line where url encoding happens.

Wondering what the best solution to doing this is...maybe a setting on the root level options encodeMultipartFilenames or allow multipart: to take an object multipart: { encodeFilenames: true?

tomas commented 4 years ago

Hi @alexburley,

I'm not sure why you'd need this but I guess we could add an option for that. So the option would essentialy skip the encodeUriComponent call or would it use iconv as well? (Seems like overkill)

alexburley commented 4 years ago

Hey @tomas basically we are interacting with an API and they do not process the filename properly if its URL encoded. So in the UI a file uploaded as foo bar.png appears as foo%20bar.png.

I agree it feels overkill, but essentially we would just need a mechanism for skipping the encodeUriComponent call.

It looks like other form-data builders (such as form-data) avoid the URL encoding and just stick the filename straight in https://github.com/form-data/form-data/blob/master/lib/form_data.js#L237.

tomas commented 4 years ago

Can't you just ask the API provider to perform decoding on the filenames? :)

But go ahead and submit a PR including the change. I like the second version you proposed, but remember that Needle uses snake_case instead of camelCase (I'm from the Ruby side of the force).

alexburley commented 4 years ago

@tomas Yeah we are trying to persuade them, especially as it's in the official spec for the receiver of the data to decode the filename properly

I'll give it a go 👍