Guide

Event Object

Event object carries an incoming request and context.

Every time a new HTTP request comes, h3 internally creates an Event object and passes it though event handlers until sending the response.

An event is passed through all the lifecycle hooks and composable utils to use it as context.

Example:

import { getQuery, readJSONBody } from "h3";

app.use(async (event) => {
  // Log event. `.toString()` stringifies to a simple string like `[GET] /<path>`
  console.log(`Request: ${event.toString()}`);

  // Parse query params
  const query = getQuery(event);

  // Try to read request body
  const body = await readJSONBody(event).catch(() => {});

  // Echo back request as response
  return {
    path: event.path,
    method: event.method,
    query,
    body,
  };
});

Properties

The main properties of an event are:

event.method

Access to the normalized (uppercase) request method.

event.path

Access to the request path. (Example: /test?test=123)

event.headers

Access to the normalized request Headers.

event.context

The context is an object that contains arbitrary information about the request. You can store your custom properties inside event.context to share across composable utils.

  • context with some context information about the request.
  • headers with a normalized version of the headers of the request.
  • handled with a boolean that indicates if the request has terminated.
You can alternatively use getRequestHeaders(event) or getRequestHeader(event, name) for a simplified interface.