requestbody-parser
v1.1.0
Published
A simple HTTP request body parser
Maintainers
Readme
reqparser
A simple HTTP request body parser.
Installation
npm i requestbody-parserUsage
import { parseReqBody, getMethodAndPath } from 'reqparser';
const reqBody = `"POST /login HTTP/1.1\r\n"
"Host: example.com\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 34\r\n"
"\r\n"
"{\"username\":\"ram\",\"password\":\"123\"}";`;
const result = parseReqBody(reqBody);
console.log(result.method); // POST
console.log(result.path); // login
console.log(result.headers); // { Host: 'example.com', ... }
console.log(result.data); // { username: 'ram', password: '123' }API
parseReqBody(body: string): ParsedRequest
Parses an HTTP request body string and returns the parsed components.
Returns:
method: HTTP method (GET, POST, etc.)path: Request pathheaders: Object containing request headersdata: Parsed JSON body (if present)
getMethodAndPath(body: string): MethodAndPath
Extracts just the method and path from an HTTP request body.
Returns:
method: HTTP methodpath: Request path
Types
interface ParsedRequest {
method: string;
path: string;
headers: Headers;
data: any;
}
interface MethodAndPath {
method: string;
path: string;
}
interface Headers {
[key: string]: string;
}License
ISC
