req

Get IP Address

Don't forget to set the proxy option if you're using Cloudflare's CDN and/or DDoS protection.

c.req.ip

Get Method

The method of the incoming request.

e.g. GET

c.req.method

Get Parameters

This method validates the parameter according to your scheme, if you have defined one. It'll throw an exception if the parameter doesn't match your specified schema.

A method to retrieve the corresponding value of a parameter.

c.req.param('...')

Get Raw Request

Retrieve the unmodified Request object.

Please note that the body stream might already be consumed.

c.req.raw

Get Body

This method has a time limit of 3s for added security. If the function cannot complete within 3s or fails to parse the body and therefore doesn't match your specified scheme, it'll throw an exception.

const body = await c.req.body()
// ZodObject
app.post('/', {
  body: z.string().min(4).max(16)
}, c => {
  console.log(await c.req.body())
})

// ZodUnion
app.post('/', {
  body: z.union([
    z.string().min(4).max(16),
    z.string().email()
  ])
}, c => {
  console.log(await c.req.body())
})

app.post('/', {
  body: z.string().min(4).max(16)
    .or(z.string().email())
}, c => {
  console.log(await c.req.body())
})

Get Cookies

This method validates the cookies according to your scheme, if you have defined one. It'll throw an exception if the cookies doesn't match your specified schema.

app.get('/', {
  cookies: z.object({
    foo: z.string()
  })
}, c => {
  const cookies = c.req.cookies // e.g. { foo: 'bar' }
})

Get Headers

This method validates the headers according to your scheme, if you have defined one. It'll throw an exception if the headers doesn't match your specified schema.

If you didn't specify a scheme for the headers, they will nevertheless be parsed and have the type Record<string, string | undefined>.

app.get('/', {
  headers: z.object({
    bar: z.string()
  })
}, c => {
  const headers = c.req.headers // e.g. { bar: 'foo' }
})

Get Query Parameters

This method validates the query parameters according to your scheme, if you have defined one. It'll throw an exception if the query parameters doesn't match your specified schema.

If you didn't specify a scheme for the query parameters, they will nevertheless be parsed and have the type Record<string, unknown>.

app.get('/', {
  query: z.object({
    foo: z.boolean()
  })
}, c => {
  const query = c.req.query // e.g. { foo: true }
})