Skip to main content

HttpContext

HttpContext is the execution context passed to every HTTP operation handler. It provides decoded and validated access to all parts of the incoming request — path parameters, query parameters, headers, cookies, and the request body — and exposes the raw Node.js request/response objects for cases that require lower-level control.

Package: @opra/http


Properties

PropertyTypeDescription
requestHttpIncomingThe incoming HTTP request. → See HttpIncoming
responseHttpOutgoingThe outgoing HTTP response. → See HttpOutgoing
pathParamsRecord<string, any>Decoded path parameters, keyed by the name declared in .PathParam().
queryParamsRecord<string, any>Decoded query string parameters, keyed by the name declared in .QueryParam().
headersRecord<string, any>Decoded request headers, keyed by the name declared in .Header().
cookiesRecord<string, any>Decoded cookies, keyed by the name declared in .Cookie().
errorsError[]Accumulated errors during request processing. The adapter checks this after the handler returns.
mediaTypeHttpMediaType | undefinedThe matched request content type definition, derived from the .RequestContent() declaration.
isMultipartbooleantrue when the request Content-Type is multipart/*.
note

pathParams, queryParams, headers, and cookies only contain parameters explicitly declared on the controller or operation. Use ctx.request to access undeclared values from the raw request.


Methods

getBody<T>()

getBody<T>(args?: { toFile: boolean | string }): Promise<T>

Reads, parses, and validates the request body. The body is cached — calling getBody() multiple times returns the same decoded value.

Supported content types out of the box: application/json, application/yaml, application/toml, text/plain, and binary. The actual decode is driven by the type declared in .RequestContent().

ParameterTypeDescription
args.toFileboolean | stringSave the raw body to a temp file instead of buffering in memory. Pass true for an auto-named file or a string for a specific path.
// JSON body — decoded against the declared RequestContent type
const body = await ctx.getBody<Customer>();

// Save a large binary upload to a temp file
const file = await ctx.getBody<LocalFile>({ toFile: true });

Throws BadRequestError if parsing or validation fails.


getMultipartReader()

getMultipartReader(): Promise<MultipartReader>

Returns a MultipartReader for streaming a multipart/form-data request. The same instance is returned on repeated calls.

if (ctx.isMultipart) {
const reader = await ctx.getMultipartReader();
const parts = await reader.getAll();
}

Throws InternalServerError if the request is not multipart.
Throws NotAcceptableError if the operation does not declare .MultipartContent().


info

ctx.request is an HttpIncoming — a Node.js IncomingMessage extended with Express-compatible helpers for headers, content negotiation, and body reading.

ctx.response is an HttpOutgoing — a Node.js ServerResponse extended with Express-compatible helpers for cookies, redirects, and response status. In most OPRA handlers you return a value instead of writing to the response directly.