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
Set Content-Type to 'application/x-www-form-urlencoded' when body is an instance of url.URLSearchParams #296
Comments
Ultimately my rationale here is that pretty much every http client library in node does this in some way or another (usually with some custom option). @TimothyGu (sorry to ping, but I know you're extra familiar with URLSearchParams) - I'm trying to figure out if just typeof this.body === 'object' && this.body.constructor.name === 'URLSearchParams' is sufficient here. The biggest advantage of this ofc is that there's no overhead and is backwards compatible. The clearly 'safer' way to do it would be URLSearchParams.prototype.get.call(this.body, '') - we can just use a synchronous try/catch, as call() will throw if this.body is not a URLSearchParams. The down side, ofc, is we'll need to import I just wanted to get the maintainers thoughts before I went down a path that would be outright rejected :) |
I agree with the necessity of this feature. The challenge is, as you have found out, detecting whether an object is a URLSearchParams object.
|
On a second thought, I guess we can combine several of those tests to form a conditional that works for many cases: function isURLSearchParams(obj) {
// Duck-typing as a necessary condition.
if (typeof obj !== 'object' ||
typeof obj.append !== 'function' ||
typeof obj.delete !== 'function' ||
typeof obj.get !== 'function' ||
typeof obj.getAll !== 'function' ||
typeof obj.has !== 'function' ||
typeof obj.set !== 'function') {
return false;
}
// Brand-checking and more duck-typing as optional condition.
return obj.constructor.name === 'URLSearchParams' ||
Object.prototype.toString.call(obj) === '[object URLSearchParams]' ||
typeof obj.sort === 'function';
} This should work for:
I think that's a sufficient number of cases covered, and we can always add more cases later. |
Tim you are a wealth of resources, as usual. I'll double check the logic as you've suggested it be implemented and hopefully have a PR with tests and readme updates by mañana. Thanks again for the support! |
Just so you know |
Seting
Content-Type
to 'application/x-www-form-urlencoded' when body is an instance ofurl.URLSearchParams
would match spec as per https://fetch.spec.whatwg.org/#concept-bodyinit-extractAs far as I can tell, this is not something that
node-fetch
currently does.. and it caught me up on what I consider to be a somewhat obscure bug for 30 minutes.Interestingly (although possibly unrelated),
curl
sets this by default when using a simple data cli arg (-d foo=bar) as opposed to using text/plain, but I guess we're more interested in the fetch spec than how curl works.If this is an acceptable proposal, I can get the PR put together in a fairly short time.
Edit: fixed incorrect whatwg link anchor
The text was updated successfully, but these errors were encountered: