You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
林华焜 b6f8f922cf
first commit
4 years ago
..
test first commit 4 years ago
README.md first commit 4 years ago
index.js first commit 4 years ago
package.json first commit 4 years ago

README.md

Native Request

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

  • supports HTTPS
  • 0 dependencies
  • use callbacks

Installation

Install the dependencies and devDependencies and start the server.

npm install native-request

Usage

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
});

Custom request

  • *.request(path, method, callback)
  • *.request(path, method,data, callback)
  • *.request(path, method, data, headers, callback) To send a PUT request:
let request = require('native-request');
request.request('https://github.com', 'PUT', 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.request('https://github.com','PUT', 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.