Skip to content

Simple HTTP request and response parser, written in pure javascript with zero dependencies

License

Notifications You must be signed in to change notification settings

dubniczky/Httparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple HTTP request and response parser, written in pure javascript with zero dependencies

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published