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:
Property | Description |
---|---|
.path | the request path |
.method | the request method |
.url | the 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 |
11: | Handling forms COMING SOON |
Are you intimidated by Git? Can’t figure out
merge vs rebase? Are you afraid of screwing up
something any time you have to do something in
Git? Do you rely on ChatGPT or random people’s
answer on StackOverflow to fix your problems?
Your coworkers are tired of explaining Git to
you all the time? Git is something we all need
to use, but few of us really master it. I
created this course to improve your Git (and
GitHub) knowledge at a radical level. Launching
May 21, 2024. Join the waiting list!