[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/core](../../README) / [index](../README) / withHeaders

# Function: withHeaders()

> **withHeaders**(`res`, `headers`): `Response`

Defined in: [http/withHeaders.ts:16](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/withHeaders.ts#L16)

Return a copy of `res` with the given headers added/overwritten.

A `Response` produced by `Response.redirect()` (or `Response.error()`) has an
**immutable** headers guard — calling `res.headers.set(...)` on it throws.
Middleware that decorates the downstream response (CORS, security headers,
rate-limit info, …) must therefore reconstruct rather than mutate in place.
`new Response(body, ...)` always yields a mutable guard, so the copy is safe.

## Parameters

### res

`Response`

### headers

`Record`\<`string`, `string`\>

## Returns

`Response`

## Example

```ts
async handle(_, next) {
  const res = await next();
  return res ? withHeaders(res, { "X-Frame-Options": "DENY" }) : res;
}
```
