Skip to main content

HttpIncoming

HttpIncoming is the incoming HTTP request interface used throughout OPRA. It extends NodeIncomingMessage with Express-compatible helpers for routing metadata, content negotiation, and body reading.

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

In operation handlers ctx.request is an HttpIncoming. See HttpContext.


Properties

PropertyTypeDescription
resHttpOutgoingThe 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

HttpIncoming.from(instance)

HttpIncoming.from(
instance: HttpIncoming | NodeIncomingMessage.Initiator | string | Iterable<any> | AsyncIterable<any>
): HttpIncoming

Factory that creates or upgrades an object into a full HttpIncoming instance. If instance is already an HttpIncoming it is returned as-is. Used by adapters and tests.