qfetch
    Preparing search index...

    Function withHeader

    • Middleware that adds a single header to outgoing fetch requests.

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

      Parameters

      • name: string

        The header name

      • value: string

        The header value

      Returns MiddlewareExecutor

      A middleware executor that adds the header to requests

      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
      • Works with both string URLs and Request objects
      import { withHeader } from "@qfetch/middleware-headers";

      // Add Content-Type header
      const qfetch = withHeader("Content-Type", "application/json")(fetch);
      await qfetch("https://api.example.com/users");
      // Request includes: Content-Type: application/json
      import { withHeader } from "@qfetch/middleware-headers";
      import { compose } from "@qfetch/core";

      // Compose multiple headers
      const qfetch = compose(
      withHeader("Content-Type", "application/json"),
      withHeader("Accept", "application/json")
      )(fetch);
      import { withHeader } from "@qfetch/middleware-headers";

      // Request headers take precedence
      const qfetch = withHeader("Content-Type", "application/json")(fetch);
      await qfetch("https://api.example.com/users", {
      headers: { "Content-Type": "text/plain" }
      });
      // Request uses: Content-Type: text/plain (request value wins)