Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/http/util.ts>parseRequest

A JavaScript extension package for building strong and modern applications.
Latest
function parseRequest
import { parseRequest } from "https://deno.land/x/ayonli_jsext@v0.9.72/http/util.ts";

Parses the text message as an HTTP request.

NOTE: This function only supports HTTP/1.1 protocol.

Examples

Example 1

// GET example
import { parseRequest } from "@ayonli/jsext/http";

const message = "GET /foo HTTP/1.1\r\nHost: example.com\r\n\r\n";
const req = parseRequest(message);

console.log(req.method); // "GET"
console.log(req.url); // "http://example.com/foo"
console.log(req.headers.get("Host")); // "example.com"

Example 2

// POST example
import { parseRequest } from "@ayonli/jsext/http";

const message = "POST /foo HTTP/1.1\r\n"
    + "Host: example.com\r\n"
    + "Content-Type: application/x-www-form-urlencoded\r\n"
    + "Content-Length: 19\r\n"
    + "\r\n"
    + "foo=hello&bar=world";
const req = parseRequest(message);

console.log(req.method); // "POST"
console.log(req.url); // "http://example.com/foo"
console.log(req.headers.get("Host")); // "example.com"

const form = new URLSearchParams(await req.text());

console.log(form.get("foo")); // "hello"
console.log(form.get("bar")); // "world"

Parameters

message: string