Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A minor change to how field names are encoded #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

phatmaus
Copy link

@phatmaus phatmaus commented Jan 7, 2016

I recently needed to use an API that couldn't handle the field names being
encoded with quotes. I added an optional parameter to FormData.append,
that can remove the quotes. Others in a similar situation might find it
useful and it can have no side effects on existing code as far as I can
see.

I recently needed to use an API that couldn't handle the field names being
encoded with quotes. I added an optional parameter to FormData.append,
that can remove the quotes. Others in a similar situation might find it
useful and it can have no side effects on existing code as far as I can
see.
@alexindigo
Copy link
Member

Thanks for your interest in the package.

Do you mind to elaborate on your use case? Seems like one-off from here, especially comparing with the number of changes.

And I see more changes than just described change.

PS. FormData meant to be "low-level" implementation, focused on sending data with the right headers and right encoding. Accommodating different users preferences (e.g. converting arrays to strings) is out of scope of this project.

@@ -159,7 +159,7 @@ FormData.prototype._trackLength = function(header, value, options) {
};

// TODO: Use request's response mime-type
FormData.prototype._multiPartHeader = function(field, value, options) {
FormData.prototype._multiPartHeader = function(field, value, options,encodeFieldNamesWithoutQuotes) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to move this parameter to the options, since it may be passed here anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, thanks

@phatmaus
Copy link
Author

phatmaus commented Jan 7, 2016

Hi Alex. Thanks for the quick response.

Is it the standard that the field names have to be surrounded by quotes? i.e. 'name="' + field + '"'
I ran into an API that cannot parse that and I figured that others may have run into a similar issue. And the module can still be used like before, since if you call .append( with 2 or 3 parameters, encodeFieldNamesWithoutQuotes? will just evaluate to false and the logic will be completely unchanged, ditto for other calls of _multiPartHeader(. It introduces a couple of lines of repetition and I did think of writing it as something like:
['form-data', 'name='+encodeFieldNamesWithoutQuotes?'"':'' + field + encodeFieldNamesWithoutQuotes?'"':'' ].concat(contentDisposition || []),
but I think that looks ugly.

} else if (value.hasOwnProperty('httpVersion')) {
next(null, +value.headers['content-length']);

// or request stream http://github.com/mikeal/request
// or request stream http://github.com/mikeal/request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind to clean up unnecessary changes like this?
And add tests for the use case?

Thank you.

@alexindigo
Copy link
Member

@phatmaus Thanks for the details. Could you put details on the server/environment in the comments block in the new test file (and mention it in the readme). I think it would help others to find solution faster than just introducing verbose flag.

Thanks.

@danielgindi
Copy link

danielgindi commented Sep 3, 2018

Based on the RFC https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1
There's no need for all of this. No options and such. You should always quote that field.

@Igorbek
Copy link

Igorbek commented Sep 4, 2018

@danielgindi correct. but quoted-string must be escaped, not just accompanied by ". So the correct change would be to escape any such characters.

@danielgindi
Copy link

@Igorbek of course!

@ibdf
Copy link

ibdf commented Sep 17, 2020

This is old, I think it was never implemented, but I ran into this same issue - a third-party api that cannot parse the quotes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants