Skip to content

baseURL with a query string puts the request path inside the query #648

Description

@trackoor

Environment

  • ofetch: 1.5.1, 2.0.0-alpha.3 (main at 1dbc37fd1c)
  • ufo: 1.6.4
  • Node.js: v26.13.0

Reproduction

// repro.mjs
import http from "node:http";
import { ofetch } from "ofetch";

const server = http.createServer((req, res) => {
  res.setHeader("content-type", "application/json");
  res.end(JSON.stringify(req.url));
}).listen(3000, async () => {
  console.log(await ofetch("/users", { baseURL: "http://localhost:3000/api?key=abc" }));
  console.log(await ofetch("/users", { baseURL: "http://localhost:3000/api?key=abc", query: { page: 2 } }));
  server.close();
});

Run node repro.mjs:

Actual output:

/api?key=abc/users
/api?key=abc%2Fusers&page=2

Expected output:

/api/users?key=abc
/api/users?key=abc&page=2

Describe the bug

The README section on baseURL states that ofetch handles "trailing/leading slashes and query search params for baseURL". However, when baseURL contains a query string, withBase(context.request, context.options.baseURL) joins the request path onto the end of the entire baseURL string (after the query parameters), so /users becomes part of the key query parameter value instead of the URL path.

Additional context

Credit: https://ucsbplse.github.io/

Logs

/api?key=abc/users
/api?key=abc%2Fusers&page=2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions