native-request
A simple package with no dependencies for native requests using callback
Last updated 5 years ago by zaral .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install native-request 
SYNC missed versions from official npm registry.

Native Request

npm version npm

v1.1 has been released

Native Request is a simple module that makes you create native node.js requests supports https.

  • supports HTTPS
  • 0 dependencies
  • use callbacks

Table of Contents

Installation
Usage Planned features

Planned features

  • 1.2.0: Proxy management

Installation

Install the dependencies and devDependencies and start the server.

npm install native-request

Usage

JSON request (recommended)

  • request.request(options, callback)

Easy

let request = require('native-request');

request.request({
        url: "http://github.com/",
        method: 'POST',
    }, function(err, data, status, headers) {
        console.log(status); //200
        console.log(data); // page content
        console.log(headers); // response headers
});

Full

let request = require('native-request');

request.request({
        url: "http://github.com/",
        method: 'POST',
        Cookies: { john: "doe", human: true },
        headers: {
            authorization: "Token121"
        },
        requestOptions: {
            followRedirect: false,
            maxRedirect: 1,
            trustRedirect: false
        }

    }, function(err, data, status, headers) {
        console.log(status); //200
        console.log(data); // page content
        console.log(headers); // response headers
});

Parameters

Options Required Type Parameters Default
url String Target url
method String HTTP method to use. More info here
Headers JSON Object Pass headers to the request with a JSON format. {"content-type": "application/json"}
Cookies JSON Object Pass cookies to the request with a JSON format
requestOptions See below

RequestOptions

The parameters below are here for client configuration. None of these parameters will be sent. These parameters must be put in the object 'requestOptions'

Options Required Type Parameters Default
followRedirect boolean Decide if we should follow the redirects true
maxRedirect int Decide the maximum number of redirects allowed 3
trustRedirect boolean If false, headers will not be sent when a redirect happen true

GET request

  • request.get(path, headers, callback)
  • request.get(path, callback)
let request = require('native-request');
request.get('https://github.com', function(err, data, status, headers) {
    if (err) {
        throw err;
    }
    console.log(status); //200
    console.log(data); // page content
    console.log(headers); // response headers
});

To add custom headers just do like this:

let request = require('native-request');

let headers = {
    "content-type": "plain/text"
}
request.get('https://github.com', headers, function(err, data, status, headers) {
    if (err) {
        throw err;
    }
    console.log(status); //200
    console.log(data); // page content
    console.log(headers); // response headers
});

POST request

  • request.post(path, callback)
  • request.post(path, data, callback)
  • request.post(path, data, headers, callback)

To send an empty post:

let request = require('native-request');
request.post('https://github.com', function(err, data, status, headers) {
    if (err) {
        throw err;
    }
    console.log(status); //200
    console.log(data); // page content
    console.log(headers); // response headers
});

With headers and data:

let request = require('native-request');

let data = {
    "example": true,
}
let headers = {
    "content-type": "plain/text"
}
request.post('https://github.com', data, headers, function(err, data, status, headers) {
    if (err) {
        throw err;
    }
    console.log(status); //200
    console.log(data); // page content
    console.log(headers); // response headers
});

License

MIT. Copyright (c) Samuel Marchese.

Current Tags

  • 1.1.2                                ...           latest (4 months ago)

13 Versions

  • 1.1.2                                ...           4 months ago
  • 1.1.1                                ...           4 months ago
  • 1.1.0                                ...           3 years ago
  • 1.0.9                                ...           3 years ago
  • 1.0.8                                ...           4 years ago
  • 1.0.7                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           5 years ago
  • 1.0.4                                ...           5 years ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Total 11
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE