Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

native-request

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

native-request

A simple package with no dependencies for native requests using callback

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
556K
decreased by-11.62%
Maintainers
0
Weekly downloads
 
Created
Source

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

  • 1.2.0: Proxy management

Installation

Install the dependencies and devDependencies and start the server.

npm install native-request

Usage

  • 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
OptionsRequiredTypeParametersDefault
urlStringTarget url
methodStringHTTP method to use. More info here
HeadersJSON ObjectPass headers to the request with a JSON format.{"content-type": "application/json"}
CookiesJSON ObjectPass cookies to the request with a JSON format
requestOptionsSee 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'

OptionsRequiredTypeParametersDefault
followRedirectbooleanDecide if we should follow the redirectstrue
maxRedirectintDecide the maximum number of redirects allowed3
trustRedirectbooleanIf false, headers will not be sent when a redirect happentrue

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.

Keywords

FAQs

Package last updated on 24 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc