httparser
v1.1.0
Published
Simple HTTP request and response parser, written in pure javascript with zero dependencies
Downloads
4
Readme
Httparser
Simple HTTP request and response parser, written in pure javascript with zero dependencies
Usage
Install the package
Use any of the following package managers
npm add httparser
yarn add httparser
pnpm add httparser
Import
Note: the package only supports ES3 imports for now
import { HTTPRequest, HTTPResponse } from 'httparser'
Parse a file
Request
import fs from 'fs'
import { HTTPRequest } from 'httparser'
const data = fs.readFileSync('example.http', 'utf8')
const request = new HTTPRequest(data)
console.log(request.method)
console.log(request.headers['Host'])
console.log(request.fragment)
console.dir(request)
Response
import fs from 'fs'
import { HTTPResponse } from 'httparser'
const data = fs.readFileSync('example.http', 'utf8')
const response = new HTTPResponse(data)
console.log(response.reason)
console.log(response.statusCode)
console.log(response.body)
console.dir(response)
Body Parsing
By default, parsing JSON and form objects are supported. They can be explicitly turned off by using the following constructor:
const req = HTTPRequest(data, false)
Otherwise, parsed JSON will be added to the json
property, and a parsed form will be added to the