qfetch
    Preparing search index...

    Function withHeaders

    • Middleware that adds multiple headers to outgoing fetch requests.

      Sets default headers on outgoing requests. Request headers take precedence over middleware headers - if a header already exists in the request, the middleware header is not applied.

      Parameters

      • headers: HeadersInput

        Object or Headers instance with header name-value pairs

      Returns MiddlewareExecutor

      A middleware executor that adds the headers to requests

      Input formats:

      • Plain object: { "Content-Type": "application/json" }
      • Headers instance: new Headers({ "Content-Type": "application/json" })

      Header names: Case-insensitive per HTTP specification. Content-Type and content-type are treated as the same header.

      Merge behavior:

      • Request headers take precedence (no override)
      • Middleware headers are only added if not already present
      • Empty headers object {} passes request through unchanged
      • Works with both string URLs and Request objects
      import { withHeaders } from "@qfetch/middleware-headers";

      // Add multiple headers with plain object
      const qfetch = withHeaders({
      "Content-Type": "application/json",
      "Accept": "application/json",
      "X-Request-ID": "abc123"
      })(fetch);

      await qfetch("https://api.example.com/users");
      import { withHeaders } from "@qfetch/middleware-headers";

      // Using Headers instance
      const defaultHeaders = new Headers();
      defaultHeaders.set("Content-Type", "application/json");
      defaultHeaders.set("Accept", "application/json");

      const qfetch = withHeaders(defaultHeaders)(fetch);
      import { withHeaders } from "@qfetch/middleware-headers";
      import { withBaseUrl } from "@qfetch/middleware-base-url";
      import { compose } from "@qfetch/core";

      // Composition with other middlewares
      const qfetch = compose(
      withHeaders({
      "Content-Type": "application/json",
      "Accept": "application/json"
      }),
      withBaseUrl("https://api.example.com")
      )(fetch);

      await qfetch("/users");