Giter Site home page Giter Site logo

http-packet's Introduction

http-packet

Build Status FOSSA Status

Coverage Status

HTTP crossplatform request generator. It allows generating and parsing HTTP 1.1 requests. May be used with net.Socket or any other transport. Working on browser and node.

How to use

Creating instance

First, we need create HttpPacket instance.

// Use import HttpPacket from '@shpingalet007/http-packet';
// or const HttpPacket = require('@shpingalet007/http-packet');

const httpPacket = new HttpPacket(/* params object */);

HttpPacket supports more options to be passed in constructor, but they would be described further. The only mandatory param is url.

What we need to know is that there are default params set:

version: '1.1' | HttpVersions.v1_1              // Currently stable on HTTP 1.1
method: 'get' | RequestMethods.Get              // HTTP Method GET
encoding: 'text/plain' | Encodings.TextPlain    // Text plain

Generating HTTP request

Example 1: Simple GET request

The library can produce HTTP requests as string or as Uint8Array binary.

const httpPacket = new HttpPacket({ url: 'http://example.net/' });
const httpRequest = httpPacket.generate('string');

This will produce GET request to http://example.net/ :

GET / HTTP/1.1\r\n
Host: example.net\r\n
\r\n

Example 2: GET request with query params

We can specify query params with constructor setting queryParams, or they might be decoded from url setting if there are any.

const httpPacket = new HttpPacket({
  url: 'http://example.net/',
  queryParams: { testParam: 'This is just a test' }
});
const httpRequest = httpPacket.generate('string');

All queryParams would be urlencoded automatically. And as a result, we get next request:

GET /?testParam=This%20is%20just%20a%20test HTTP/1.1\r\n
Host: example.net\r\n
\r\n

Example 3: POST request with data in body

In this example we would change the HTTP method and set some data in body

const httpPacket = new HttpPacket({
  method: 'POST',
  url: 'http://example.net/',
  body: {  
    encoding: /* 'application/json' || Encodings.Json, */
    content: { testParam: 'This is just a test' }
  }
});
const httpRequest = httpPacket.generate('string');

And we get next request:

POST / HTTP/1.1\r\n
Host: example.net\r\n
Content-Type: application/json\r\n
Content-Length: 35\r\n
\r\n
{"testParam":"This is just a test"}

Example 4: Complex request

Alright, here we would make a conclusion creating complex request with all settings available in HttpPacket library.

const httpPacket = new HttpPacket({  
  version: HttpVersions.v2_0,  
  authentication: {  
    type: AuthTypes.Basic,  
    credentials: { username: 'admin', password: '0000' },  
  },  
  method: RequestMethods.Post,  
  url: 'http://example.net/',  
  queryParams: {  
    q1: 'test',  
  },  
  headers: {  
    // 'X-Application': 'Browser',  
    xApplication: 'Browser', // This will result in 'X-Application' header
    _xApplication: 'Browser', // This will result in 'xApplication' header
  },  
  body: {  
    encoding: Encodings.FormUrlencoded,  
    content: {  
      a: '100',  
      b: '200',  
    },  
  },  
});

Resulting in next packet:

POST /?q1=test HTTP/2.0\r\n
Host: example.net\r\n
Content-Type: application/x-www-form-urlencoded\r\n
X-Application: Browser\r\n
xApplication: Test header\r\n
Authorization: Basic YWRtaW46MDAwMA==\r\n
Content-Length: 11\r\n
\r\n
a=100&b=200

HttpPacket settings

Settings are passed on instance construction. Full list is next:

version: typeof HttpVersions (default: HttpVersions.v1_1)
authentication: AuthBasic | AuthBearer (default: null)
method: typeof RequestMethods (default: RequestMethods.Get)
url: string, (mandatory param)
queryParams: object, (default: null)
headers: object, (default: null)
body: ApplicationJsonBody | FormDataBody | UrlencodedBody | TextPlainBody

License

FOSSA Status

http-packet's People

Contributors

fossabot avatar shpingalet007 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.