https://github.com/dubniczky/httparser
Simple HTTP request and response parser, written in pure javascript with zero dependencies
https://github.com/dubniczky/httparser
http javascript no-dependencies pure-javascript request response
Last synced: about 1 year ago
JSON representation
Simple HTTP request and response parser, written in pure javascript with zero dependencies
- Host: GitHub
- URL: https://github.com/dubniczky/httparser
- Owner: dubniczky
- License: isc
- Created: 2022-11-16T23:35:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-25T23:17:50.000Z (over 3 years ago)
- Last Synced: 2025-03-01T03:26:16.216Z (about 1 year ago)
- Topics: http, javascript, no-dependencies, pure-javascript, request, response
- Language: JavaScript
- Homepage:
- Size: 60.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
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
```bash
npm add httparser
yarn add httparser
pnpm add httparser
```
### Import
Note: the package only supports ES3 imports for now
```js
import { HTTPRequest, HTTPResponse } from 'httparser'
```
### Parse a file
Request
```js
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
```js
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:
```js
const req = HTTPRequest(data, false)
```
Otherwise, parsed JSON will be added to the `json` property, and a parsed form will be added to the