Skip to content

Request Headers Plugin

Use RequestHeadersHandlerPlugin to expose incoming request headers as context.reqHeaders.

Context Access

ts
import { 
getCookie
} from '@orpc/server/helpers'
import type { RequestHeadersHandlerPluginContext } from '@orpc/server/plugins' interface ServerContext extends RequestHeadersHandlerPluginContext {} const
base
=
os
.
$context
<ServerContext>()
const
example
=
base
.
use
(({
context
,
next
}) => {
const
sessionId
=
getCookie
(
context
.
reqHeaders
, 'session_id')
return
next
()
}) .
handler
(({
context
}) => {
const
userAgent
=
context
.
reqHeaders
?.
get
('user-agent')
return {
userAgent
}
})

Why can reqHeaders be undefined?

This allows procedures to run safely even without RequestHeadersHandlerPlugin, such as in direct calls.

TIP

Combine with Cookie Helpers for streamlined cookie management.

Handler Setup

ts
import { RequestHeadersHandlerPlugin } from '@orpc/server/plugins'

const handler = new RPCHandler(router, {
  plugins: [
    new RequestHeadersHandlerPlugin(),
  ],
})

INFO

The handler can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or a custom one.

Learn More

For implementation details, see the source code.

Released under the MIT License.