Skip to main content

HttpRequest

HttpRequest is the interface for incoming HTTP requests in OPRA. It extends Node.js http.IncomingMessage with Express-compatible helpers for routing metadata, content negotiation, and body reading.

import type { HttpRequest } from '@opra/http';

In operation handlers, ctx.request is an HttpRequest. See HttpContext.

The runtime implementation is provided by HttpRequestHost, which is mixed into a plain http.IncomingMessage via HttpRequest.create(). You do not construct or import HttpRequestHost directly.


Properties​

PropertyTypeDescription
resHttpResponseThe paired response object.
baseUrlstringThe URL prefix at which the router is mounted.
originalUrlstringThe full original URL before any path rewriting.
protocolstring'http' or 'https'. Respects X-Forwarded-Proto when trust proxy is enabled.
ipstringRemote IP address. Respects trusted proxy headers.
ipsstring[]IP addresses in the proxy chain when trust proxy is enabled.
securebooleantrue when protocol === 'https'.
secretstring | undefinedUsed for signed cookies.
hostnamestring | undefinedHostname from the Host header. Respects X-Forwarded-Host when trust proxy is enabled.
freshbooleantrue when Last-Modified and/or ETag headers match, indicating the client cache is current.
paramsRecord<string, any>Raw path parameter values from the router.
cookiesRecord<string, any> | undefinedRaw cookies (populated when cookie-parser middleware is used).

Methods​

header(name) / get(name)​

header(name: string): string | undefined
get(name: string): string | undefined

Returns the value of a named request header. Case-insensitive. 'Referrer' and 'Referer' are treated as equivalent.

const token = ctx.request.header('authorization');
const ct = ctx.request.get('content-type');

is(type)​

is(type: string | string[]): string | false | null

Checks whether the Content-Type header matches the given MIME type, extension, or wildcard pattern.

ctx.request.is('json') // 'json' or false
ctx.request.is(['json', 'html'])
ctx.request.is('multipart/*')

accepts(...types)​

accepts(): string[]
accepts(type: string): string | false
accepts(type: string[]): string | false
accepts(...type: string[]): string | false

Content negotiation based on the Accept header. Returns the best match from the given types, or false.

Similarly: acceptsCharsets(), acceptsEncodings(), acceptsLanguages(), characterEncoding().


range(size, options?)​

range(size: number, options?: RangeParserOptions): RangeParserRanges | RangeParserResult | undefined

Parses the Range header for range requests. Returns ranges capped to size, or undefined if the header is absent.


readBody(options?)​

readBody(options?: BodyReader.Options): Promise<string | Buffer | undefined>

Reads the raw request body. Used internally by HttpContext.getBody(). Prefer ctx.getBody() in operation handlers.


Namespace​

HttpRequest.create(instance)​

HttpRequest.create(
instance: http.IncomingMessage | IncomingMessageHost.Initiator
): HttpRequest

Upgrades a plain http.IncomingMessage (or an IncomingMessageHost.Initiator plain object) into a full HttpRequest by mixing in HttpRequestHost methods. If instance is already an HttpRequest, it is returned as-is.

import http from 'http';
import { HttpRequest } from '@opra/http';

const raw = new http.IncomingMessage(socket);
const req = HttpRequest.create(raw);

Used by adapters and in tests. You rarely need to call this directly in application code.