> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
> Use this file to discover all available pages before exploring further.

# OutgoingMessage Header Methods

> Replace private _headers and _headerNames fields with public getHeaders() and getRawHeaderNames() methods (DEP0066)

This recipe migrates code from using deprecated private header fields (`_headers`, `_headerNames`) on `OutgoingMessage.prototype` to their public API equivalents.

## Deprecation

Node.js deprecated direct access to private header fields. Use the public methods `getHeaders()` and `getRawHeaderNames()` instead.

See [DEP0066](https://nodejs.org/api/deprecations.html#dep0066-outgoingmessageprototype_headers-outgoingmessageprototype_headernames) for more details.

## Usage

Run this codemod with:

```bash theme={null}
npx codemod nodejs/http-outgoingmessage-headers
```

## Before/After

```diff theme={null}
  const http = require('http');

  function handler(req, res) {
    res.setHeader('content-type', 'application/json');
    res.setHeader('x-custom-header', '42');

    console.log({
-     headers: res._headers,
+     headers: res.getHeaders(),
-     headerNames: res._headerNames,
+     headerNames: res.getRawHeaderNames(),
-     customHeader: res._headers['x-custom-header'],
+     customHeader: res.getHeaders()['x-custom-header'],
-     count: Object.keys(res._headers).length,
+     count: Object.keys(res.getHeaders()).length,
    });

    res.end('Hello World');
  }

  const server = http.createServer(handler);

  server.listen(3000, () => {
    console.log('Server running on port 3000');
  });
```

## What It Does

* Replaces `res._headers` with `res.getHeaders()`
* Replaces `res._headerNames` with `res.getRawHeaderNames()`
* Updates property access and method calls throughout your codebase
* Works with `ServerResponse` and other `OutgoingMessage` subclasses

## Migration Guide

| Deprecated         | Replacement               |
| ------------------ | ------------------------- |
| `res._headers`     | `res.getHeaders()`        |
| `res._headerNames` | `res.getRawHeaderNames()` |

<Warning>
  Direct access to private fields (prefixed with `_`) is not part of the public API and can break without notice. Always use public methods.
</Warning>

## References

* [http.getHeaders()](https://nodejs.org/api/http.html#requestgetheaders)
* [http.getRawHeaderNames()](https://nodejs.org/api/http.html#requestgetheadername)
