http-z
TypeScript icon, indicating that this package has built-in type declarations

7.1.1 • Public • Published

http-z

HTTP message (request/response) parser/builder according to the rules defined in RFC 7230

Works in Node.js and in the browser.

Build Status Code Coverage License npm version

Install

$ pnpm i http-z

Usage

const httpZ = require('http-z')

const plainMessage = [
  'GET /features?p1=v1 HTTP/1.1',
  'Host: example.com',
  'Accept: *',
  'Accept-Encoding: gzip,deflate',
  'Accept-Language: en-US;q=0.6, en;q=0.4',
  '',
  ''
].join('\r\n')

const messageModel = httpZ.parse(plainMessage)
console.log(JSON.stringify(messageModel, null, 2))

/* output:
{
  "protocolVersion": "HTTP/1.1",
  "method": "GET",
  "target": "/features?p1=v1",
  "host": "example.com",
  "path": "/features",
  "headersSize": 135,
  "bodySize": 0,
  "queryParams": [
    { "name": "p1", "value": "v1" }
  ],
  "headers": [
    { "name": "Host", "value": "example.com" },
    { "name": "Accept", value": "*" },
    { "name": "Accept-Encoding", "value": "gzip,deflate" },
    { "name": "Accept-Language", "value": "en-US;q=0.6, en;q=0.4" }
  ]
}
*/

const plainMessageParsed = httpZ.build(messageModel)
console.log(plainMessageParsed)

/* output:
GET /features?p1=v1 HTTP/1.1
Host: example.com
Accept: *
Accept-Encoding: gzip,deflate
Accept-Language: en-US;q=0.6, en;q=0.4


*/

API

parse(rawMessage, opts)

Parses HTTP request/response raw message and returns a model.

  • rawMessage is HTTP raw message.
  • opts - options object, can be skipped.
build(messageModel, opts)

Builds HTTP request/response raw message from the model.

  • messageModel is HTTP message model.
  • opts - options object, can be skipped.

References

License

Licensed under the MIT license.

Author

Alexander Mac

Package Sidebar

Install

npm i http-z

Weekly Downloads

1,036

Version

7.1.1

License

MIT

Unpacked Size

67.8 kB

Total Files

19

Last publish

Collaborators

  • alexandermac