Skip to content

HonoRequest

reference
2 min readUpdated

Kind: Class

Source: src/request.ts

HonoRequest represents the incoming request exposed through Hono's context. It reads decoded route parameters with param() and URL query values with query(), while keeping request parsing behavior inside the framework.

Methods

MethodSignatureReturns
paramparam(key: string extends P ? never : P2 extends ${infer _}? ? never : P2)string
paramparam(key: P2)`string
paramparam(key: string)`string
paramparam()Simplify<UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>>
paramparam(key: string)unknown
#getDecodedParam#getDecodedParam(key: string)`string
#getAllDecodedParams#getAllDecodedParams()Record<string, string>
#getParamValue#getParamValue(paramKey: any)`string
queryquery(key: string)`string
queryquery()Record<string, string>
queryquery(key: string)void
queriesqueries(key: string)`string[]
queriesqueries()Record<string, string[]>
queriesqueries(key: string)void
headerheader(name: RequestHeader)`string
headerheader(name: string)`string
headerheader()`Record<RequestHeader
headerheader(name: string)void
parseBodyparseBody(options: Options)Promise<T>
parseBodyparseBody(options: Partial<ParseBodyOptions>)Promise<T>
parseBodyparseBody(options: Partial<ParseBodyOptions>)void
jsonjson()Promise<T>
texttext()Promise<string>
arrayBufferarrayBuffer()Promise<ArrayBuffer>
bytesbytes()Promise<Uint8Array>
blobblob()Promise<Blob>
formDataformData()Promise<FormData>
addValidatedDataaddValidatedData(target: keyof ValidationTargets, data: {})void
validvalid(target: T)InputToDataByTarget<I, T>
validvalid(target: keyof ValidationTargets)void

Properties

PropertyType
rawRequest
#validatedData`{ [K in keyof ValidationTargets]?: {} }
#matchResultResult<[unknown, RouterRoute]>
routeIndexnumber
pathstring
bodyCacheBodyCache
#cachedBodyany

Where it refuses work

  • HonoRequest stops the work with an early return when name.
  • HonoRequest stops the work with an early return when cachedBody.

Diagram

mermaid
graph LR
  Request[Incoming Request] --> HonoRequest
  Route[Matched Route] --> HonoRequest
  HonoRequest --> Param[param()]
  HonoRequest --> Query[query()]
  Param --> Handler[Route Handler]
  Query --> Handler

Usage

ts
import { Hono } from 'hono'

const app = new Hono()

app.get('/users/:id', (c) => {
  const id = c.req.param('id')
  const filter = c.req.query('filter')
  const query = c.req.query()

  return c.json({
    id,
    filter,
    query,
  })
})

export default app

AI Coding Instructions

  • Access HonoRequest through c.req in route handlers rather than constructing it directly.
  • Use param('name') when reading a matched route parameter; account for undefined when the parameter may not exist.
  • Use param() without an argument when the route's typed parameter record is needed.
  • Use query('name') for a single query value and query() for the full query record.
  • Keep parameter decoding in the request class; do not call private #getDecodedParam, #getAllDecodedParams, or #getParamValue outside this class.

Relationships

  • IMPORTS → HTTPException
  • IMPORTS → GET_MATCH_RESULT
  • IMPORTS → parseBody
  • IMPORTS → getQueryParam
  • IMPORTS → getQueryParams
  • IMPORTS → tryDecodeURIComponent

Used by

1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (1)

  • Datasrc/context.ts:26

Was this page helpful?

Download as PDF
HonoRequest — Hono (narrator proof)