-
Describe the bug
Let's say we want to create an instance (using const got = require('got');
const fetch = got.extend({
prefixUrl: "https://example.org/foo/",
searchParams: new URLSearchParams({ foo: "bar" }),
hooks: {
beforeRequest: [
({ url }) => console.log('Making request with url: %s', url)
]
}
});
(async () => {
const url = "path?opt1=1&opt2=2";
try {
const response = await fetch(url);
} catch (e) {
console.error(e.message);
}
})(); Actual behaviorMerged searchParams and the whole request URL looks like: Expected behaviorI believe it should take all searchParams into consideration, so: I noticed none of docs examples mention passing query params as part of the first param so it's not clear to me whether it's a bug or a feature. Regardless, this works with other HTTP clients in JS ecosystem. Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
It's not a bug. The reason behind that behavior is that a The only thing I think we could argue is what Got has to keep, the new search string or the old one, the actual behavior is due to the merge of the options and the prevalence of BTW: if you use searchParams in both cases they should merge. |
Beta Was this translation helpful? Give feedback.
-
Hm, right, ok. The bug/feature discussion then comes down to the design decision whether Thanks. |
Beta Was this translation helpful? Give feedback.
It's not a bug.
The reason behind that behavior is that a
quuery string
(aka.search string
) (what comes in an URI after the path between?
and#
) it's not necessary a "list" of search params.URLSearchParams
is defined by WHATWG, but thequery string
can be whatever you want (eg.http://example.com/?randomString
).The only thing I think we could argue is what Got has to keep, the new search string or the old one, the actual behavior is due to the merge of the options and the prevalence of
searchParams
over thequery string
https://github.com/sindresorhus/got#urlBTW: if you use searchParams in both cases they should merge.