Hono: The Request object

I mentioned how we can inspect the request object in any request coming in to the server using c.req

The Request object holds all the HTTP request information.

Typically you have 4 kinds of request body types:

  • text/plain
  • application/json
  • application/x-www-form-urlencoded
  • multipart/form-data (when uploading files)

In a simple text request you can retrieve the request body using await c.req.text()

app.post('/', async c => { 
  const body = await c.req.text()
})

If the request body is JSON, use await c.req.json() to parse the JSON body content in an object:

app.post('/', async c => { 
  const body = await c.req.json()
})

Otherwise, for application/x-www-form-urlencoded and multipart/form-data you can retrieve the request body using the await parseBody() method.

app.post('/', async c => { 
  const body = await c.req.parseBody()
})

These are the main properties you’ll likely use:

PropertyDescription
.paththe request path
.methodthe request method
.urlthe request URL (protocol, host, port, path)
.param(“key”)retrieve a path parameter value, used in dynamic routes
.query(“key”)retrieve a specific query string parameter value
.queries()retrieve all the query string parameter values, in an array
.header(“key”)retrieve a specific request header parameter value

In addition to those, you can use all the Request object methods and properties.

Lessons in this unit:

0: Introduction
1: Your first Hono app
2: ▶︎ The Request object
3: Send a response to the client
4: Manage cookies
5: Work with HTTP headers
6: Handling redirects
7: Routing
8: JSX templates
9: Middleware
10: Hono on Node.js