Remix
Remix
remix/router · ClassView Source

RequestContext

Summary

A context object that contains information about the current request. Every request handler or middleware in the lifecycle of a request receives the same context object.

Signature

class RequestContext<params, entries> {
  constructor(request: Request): RequestContext<params, entries>;

  // Properties
  method: string;
  params: params;
  request: Request;
  url: URL;

  // Accessors
  get headers(): Headers;
  get router(): Router<RequestContext<any, entries>>;

  // Methods
  get<key extends object>(key: key): ResolveEntryValue<entries, key>;
  has<key extends object>(key: key): boolean;
  set<key extends object>(
    key: key,
    value: ContextValue<key>,
    options: { property: string },
  ): void;
}

Constructor

Parameters

request

The incoming request

Properties

method

The request method. This may differ from request.method when using the methodOverride middleware, which allows HTML forms to simulate RESTful API request methods like PUT and DELETE using a hidden input field.

params

Params that were parsed from the URL.

request

The original request that was dispatched to the router.

Note: Various properties of the original request may not be available or may have been modified by middleware. For example, the request's body may already have been consumed by the formData middleware (available as context.get(FormData)), or its method may have been overridden by the methodOverride middleware (available as context.method). You should default to using properties of the context object instead of the original request. However, the original request is made available in case you need it for some edge case.

url

The URL of the current request.

Accessors

headers

A mutable copy of the request headers.

router

The router handling this request.

Methods

get<key extends object>(key: key): ResolveEntryValue<entries, key>

Get a value from request context.

has<key extends object>(key: key): boolean

Check whether a value exists in request context.

set<key extends object>(key: key, value: ContextValue<key>, options: { property: string }): void

Set a value in request context.

Parameters

key
value

The value to write

options

Options for installing the value as a direct context property