Skip to main content

HttpOutgoing

HttpOutgoing is the outgoing HTTP response interface used throughout OPRA. It extends NodeOutgoingMessage with Express-compatible helpers for cookies, redirects, content type, and response status.

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

In operation handlers ctx.response is an HttpOutgoing. See HttpContext. In most OPRA handlers you return a value rather than writing to the response directly — use ctx.response only when you need full control.


Methods

status(code)

status(code: number): this

Sets the HTTP status code. Chainable.

ctx.response.status(201).send(newCustomer);

send(body?)

send(body?: any): this

Sends the response. Accepts a string, Buffer, or object (serialized to JSON).


sendStatus(code)

sendStatus(code: number): this

Sets the status code and sends the standard status text as the response body.

ctx.response.sendStatus(204); // → "No Content"

redirect(url) / redirect(status, url)

redirect(url: string): void
redirect(status: number, url: string): void

Sends a redirect response. Defaults to 302 when status is omitted.

ctx.response.redirect(301, 'https://new.example.com/api');

attachment(filename?)

attachment(filename?: string): this

Sets the Content-Disposition header to attachment, triggering a file download in the browser.

ctx.response.attachment('report.pdf').send(pdfBuffer);

cookie(name, val, options?) / clearCookie(name, options?)

cookie(name: string, val: any, options?: CookieOptions): this
clearCookie(name: string, options?: CookieOptions): this

Sets or clears a Set-Cookie response header. CookieOptionssee Interfaces.


contentType(type)

contentType(type: string): this

Sets the Content-Type header. Accepts a MIME type, extension ('json', 'html'), or dotted extension ('.png').


location(url)

location(url: string): this

Sets the Location response header.


links(links: Record<string, string>): this

Sets the Link response header from a map of relation → URL pairs.

ctx.response.links({
next: '/api/customers?skip=20',
prev: '/api/customers?skip=0',
});

Header management

setHeader(name: string, value: number | string | readonly string[]): this
getHeader(name: string): number | string | string[] | undefined
appendHeader(name: string, value: string | readonly string[]): this

Standard header management. setHeader replaces; appendHeader adds to an existing value.


Namespace

HttpOutgoing.from(instance)

HttpOutgoing.from(
instance: HttpOutgoing | NodeOutgoingMessage.Initiator
): HttpOutgoing

Factory that creates or upgrades an object into a full HttpOutgoing instance. If instance is already an HttpOutgoing it is returned as-is.


Interfaces

CookieOptions

OptionTypeDescription
secretstringSecret used for signing.
maxAgenumberMax-age in milliseconds.
signedbooleanSign the cookie value.
expiresDateAbsolute expiry date.
httpOnlybooleanPrevents client-side JavaScript access.
pathstringCookie path. Defaults to '/'.
domainstringCookie domain.
securebooleanOnly sent over HTTPS.
encode(val: string) => stringCustom encoding function for the cookie value.
sameSiteboolean | 'lax' | 'strict' | 'none'SameSite policy.