Skip to content

Request


Request Headers


getRequestHeaders

Get the request headers

ts
import { eventHandler, getRequestHeaders } from "vinxi/http";

export default eventHandler(async (event) => {
  const headers = getRequestHeaders(event); 
});
Signature
ts
export function getRequestHeaders(event: HTTPEvent): RequestHeaders;

getRequestHeader

Get a request header by name

ts
import { eventHandler, getRequestHeader } from "vinxi/http";

export default eventHandler(async (event) => {
  const header = getRequestHeader(event, "content-type"); 
});
Signature
ts
export function getRequestHeader(
  event: HTTPEvent,
  name: HTTPHeaderName,
): RequestHeaders[string];

Request Body


readBody

Reads request body and tries to safely parse as JSON using destr.

ts
import { eventHandler, readBody } from "vinxi/http";

export default eventHandler(async (event) => {
  const body = await readBody(event); 
});
Signature
ts
function readBody<
  T,
  Event extends H3Event<EventHandlerRequest> = H3Event<EventHandlerRequest>,
  _T = InferEventInput<"body", Event, T>,
>(event: Event, options?: { strict?: boolean }): Promise<_T>;

readFormData

Constructs a FormData object from an event, after converting it to a a web request.

ts
import { eventHandler, readFormData } from "vinxi/http";

export default eventHandler(async (event) => {
  const formData = await readFormData(event); 
  const email = formData.get("email");
  const password = formData.get("password");
});
Signature
ts
export function readFormData(event: HTTPEvent): Promise<FormData>;

readMultipartFormData

Tries to read and parse the body of an HTTPEvent as a multipart form.

ts
import { eventHandler, readMultipartFormData } from "vinxi/http";

export default eventHandler(async (event) => {
  const data = await readMultipartFormData(event); 
});
Signature
ts
type MultiPartData = {
  data: Buffer;
  name?: string;
  filename?: string;
  type?: string;
};

export function readMultipartFormData(
  event: HTTPEvent,
): Promise<MultiPartData[] | undefined>;

readValidatedBody

Tries to read the request body via readBody, then uses the provided validation function and either throws a validation error or returns the result.

Using zod

ts
import { eventHandler, readValidatedBody } from "vinxi/http";
import { z } from "zod";

const objectSchema = z.object({
  email: z.string(),
  password: z.string(),
});
//
export default eventHandler(async (event) => {
  const body = await readValidatedBody(event, objectSchema.safeParse);
});

Using custom validation function

ts
import { eventHandler, readValidatedBody } from "vinxi/http";

//
export default eventHandler(async (event) => {
  // With a custom validation function
  const body = await readValidatedBody(event, (body) => {
    return typeof body === "object" && body !== null;
  });
});
Signature
ts
export function readValidatedBody<
  T,
  Event extends HTTPEvent = HTTPEvent,
  _T = InferEventInput<"body", Event, T>,
>(event: Event, validate: ValidateFunction<_T>): Promise<_T>;

readRawBody

Reads body of the request and returns encoded raw string (default), or Buffer if encoding is falsy.

ts
import { eventHandler, readRawBody } from "vinxi/http";

export default eventHandler(async (event) => {
  const body = await readRawBody(event, "utf-8"); 
});
Signature
ts
function readRawBody<E extends Encoding = "utf8">(
  event: HTTPEvent,
  encoding?: E,
): E extends false ? Promise<Buffer | undefined> : Promise<string | undefined>;

getRequestWebStream

Captures a ReadableStream from a request.

ts
import { eventHandler, getRequestWebStream } from "vinxi/http";

export default eventHandler(async (event) => {
  const stream = getRequestWebStream(event); 
});
Signature
ts
// @lib: es2015
// @filename: index.d.ts
import { HTTPEvent } from "vinxi/http";
import { ReadableStream } from "vinxi/types/web";

// ---cut---
export function getRequestWebStream(
  event: HTTPEvent,
): ReadableStream | undefined;

Request Query


getQuery

Get the query

ts
import { eventHandler, getQuery } from "vinxi/http";

export default eventHandler(async (event) => {
  const query = getQuery(event); 
});
Signature
ts
export function getQuery<
  T,
  Event extends HTTPEvent = HTTPEvent,
  _T = Exclude<InferEventInput<"query", Event, T>, undefined>,
>(event: Event): _T;

getValidatedQuery

Get the validated query

ts
import { eventHandler, getValidatedQuery } from "vinxi/http";

export default eventHandler(async (event) => {
  const query = await getValidatedQuery(event, (query) => {
    return typeof query === "object" && query !== null;
  }); 
});
Signature
ts
export function getValidatedQuery<
  T,
  Event extends HTTPEvent = HTTPEvent,
  _T = InferEventInput<"query", Event, T>,
>(event: Event, validate: ValidateFunction<_T>): Promise<_T>;

Request Info


getRequestHost

Get the request host

ts
import { eventHandler, getRequestHost } from "vinxi/http";

export default eventHandler(async (event) => {
  const host = getRequestHost(event); 
});
Signature
ts
export function getRequestHost(
  event: HTTPEvent,
  opts?: {
    xForwardedHost?: boolean;
  },
): string;

getRequestProtocol

Get the request protocol, whether its http or https.

ts
import { eventHandler, getRequestProtocol } from "vinxi/http";

export default eventHandler(async (event) => {
  const protocol = getRequestProtocol(event); 
});
Signature
ts
export function getRequestProtocol(
  event: HTTPEvent,
  opts?: {
    xForwardedProto?: boolean;
  },
): "https" | "http";

getRequestURL

Get the request URL

ts
// @noErrors
import { eventHandler, getRequestURL } from "vinxi/http";

export default eventHandler(async (event) => {
  const url = getRequestURL(event);

  url.protocol; // "http"
  url.hostname; // "localhost"
  url.port; // "3000"
  url.host; // "localhost:3000"
  url.username; // ""
  url.password; // ""
  url.origin; // "http://localhost:3000"
  url.pathname; // "/products"
  url.search; // "?category=shoes"
  url.searchParams; // URLSearchParams { category: "shoes" };
  url.hash; // "#section";
  url.href; // "http://localhost:3000/products?category=shoes#section";
});
Signature
ts
export function getRequestURL(
  event: HTTPEvent,
  opts?: {
    xForwardedHost?: boolean;
    xForwardedProto?: boolean;
  },
): URL;

getRequestIP

Get the request IP, if visible.

ts
import { eventHandler, getRequestIP } from "vinxi/http";

export default eventHandler(async (event) => {
  const ip = getRequestIP(event); 
});
Signature
ts
export function getRequestIP(
  event: HTTPEvent,
  opts?: {
    xForwardedFor?: boolean;
  },
): string | undefined;

isPreflightRequest

Check if the request is a CORS preflight request

ts
import { eventHandler, isPreflightRequest } from "vinxi/http";

export default eventHandler(async (event) => {
  const isPreflight = isPreflightRequest(event); 
});
Signature
ts
export function isPreflightRequest(event: HTTPEvent): boolean;

getWebRequest

Get a Web Fetch API compliant Request instance from the HTTPEvent

ts
import { eventHandler, getWebRequest } from "vinxi/http";

export default eventHandler(async (event) => {
  const request = getWebRequest(event); 

  request.url;
  request.method;
  request.headers;
  request.json();
  request.formData();
  request.text();
  request.arrayBuffer();
  request.blob();
});
Signature
ts
export function getWebRequest(event: HTTPEvent): Request;