Skip to content

Fetch strips trailing question mark from the URL, doesn't match browser behaviour #4836

@pimterry

Description

@pimterry

Bug Description

Given a query string consisting of just a question mark (but no actual parameters), Undici fetch drops the query string entirely, while browser fetch does not.

Reproducible By

fetch("https://testserver.host/echo?").then(async (r) => console.log(await r.text()))

If you go to https://testserver.host/ and run this in the console, using browser native fetch, you'll see:

GET /echo? HTTP/1.1
Host: testserver.host
...

If you run it with Undici fetch, you'll see:

GET /echo HTTP/1.1
host: testserver.host
...

Expected Behavior

The question mark should be preserved. It's part of the URL, it's perfectly valid, servers can interpret it differently to a query string if they want to, it's what everybody will expect, and we should match fetch behaviour if possible.

Environment

Confirmed with latest Firefox & Chrome vs Undici v7.22.0

Additional context

I suspect this is because WHATWG URL has this same behaviour when parsing URLs for both fragments & query strings, and we're using this to extract the query string. We'll need to check that more carefully. Node-fetch had the same problem, fixed here: node-fetch/node-fetch#782

There is discussion in WHATWG about this as a general problem in URL parsing, see whatwg/url#779 for more info.

I'm happy to work on this, I'll take a look sometime this week but let me know if there's any objections or somebody else working on this or similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions