A simple HTTP request body parser
npm install requestbody-parserA simple HTTP request body parser.
``bash`
npm i requestbody-parser
`typescript
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' }
`
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)
Extracts just the method and path from an HTTP request body.
Returns:
- method: HTTP methodpath
- : Request path
`typescript
interface ParsedRequest {
method: string;
path: string;
headers: Headers;
data: any;
}
interface MethodAndPath {
method: string;
path: string;
}
interface Headers {
[key: string]: string;
}
``
ISC