Object or Headers instance with header name-value pairs
A middleware executor that adds the headers to requests
Input formats:
{ "Content-Type": "application/json" }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:
{} passes request through unchangedimport { 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");
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.