Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
/ request Public archive

☁️🌐 Easy and Powerful HTTP Requestsβ€” Simplified

Notifications You must be signed in to change notification settings

joeymalvinni/request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

*Note: At this time, the module is not named, therefore, the name request is used to represent this module.

Request

☁️🌐 Easy and Powerful HTTP Requestsβ€” Simplified

Build Status Known Vulnerabilities PRs Welcome Β 
Β 
Β 
Β 

Table of Contents


Β 

Usage

// require the request module
const request = require('request')

// supports promise chaining
// use the built-in .json() function to return a JSON object
request.get('http://ip.jsontest.com/?callback=showMyIP').json().then(console.log)
//=> showMyIP({"ip": "127.0.0.1"});

// or use an asynchronous function to handle requests
async function get(){
    let response = await request.get('http://ip.jsontest.com/?callback=showMyIP').json()
    console.log(get())
    //=> showMyIP({"ip": "127.0.0.1"});
}
get()

Β 

Docs

Functions

Β 

get               request.get(url, headers)

Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use .buffer().

    .text()
    Returns the get response result as a string.
        Example:

request.get('https://postman-echo.com/get').text().then(console.log)

    .json()
    Returns the response result as a JSON object.
        Example:

request.get('https://postman-echo.com/get').json().then(console.log)

    .buffer()
    Returns the get response result as a buffer.
        Example:

request.get('https://postman-echo.com/get').buffer().then(console.log)

    .error()
    Useful to check if a request returns an error without stopping the Node proccess, returns undefined otherwise.
        Example:

console.log(request.get('https://postman-echo.com/get').error())
//=> undefined

Β 

post              request.post(url, headers)

Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use .buffer().

    .text()
    Returns the post response result as a string.
        Example:

 request.post('https://postman-echo.com/post', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).text().then(console.log)

    .json()
    Returns the response result as a JSON object.
        Example:

 request.post('https://postman-echo.com/post', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).json().then(console.log)

    .buffer()
    Returns the get response result as a buffer.
        Example:

request.post('https://postman-echo.com/post', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).buffer().then(console.log)

    .error()
    Useful to check if a request returns an error without stopping the Node proccess, returns undefined otherwise.
        Example:

console.log(request.post('https://postman-echo.com/post', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).error())
//=> undefined

Β 

head               request.head(url, headers)

Returns the requested resource's response headers.

    .text()
    Returns the get response result as a string.
        Example:

request.head('https://www.npmjs.com/').headers().then((res)=>{
    console.log("NPM's status code: " + res.statusCode)
})

Β 

put              request.put(url, headers)

Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use .buffer().

    .text()
    Returns the put response result as a string.
        Example:

request.put('https://postman-echo.com/put', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).text().then(console.log)

    .json()
    Returns the response result as a JSON object.
        Example:

request.put('https://postman-echo.com/put', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).json().then((res)=>{
    console.log(res.data)
})

    .buffer()
    Returns the get response result as a buffer.
        Example:

request.put('https://postman-echo.com/put', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).buffer().then(console.log)

    .error()
    Useful to check if a request returns an error without stopping the Node proccess, returns undefined otherwise.
        Example:

request.put('https://postman-echo.com/put', {
    data: JSON.stringify({
        user: 'request',
        password: 'requestx@node',
        email: 'requestx@npm.org'
    })
}).error().then(console.log)
//=> undefined

Β 

proxy              request.proxy(proxyUrl)

Proxies a request. Format the string like protocol://proxyIp:proxyPort (e.g. http://127.0.0.1:3000). Returns the type of request you want to make. Then, request the resource like normal.

Please note:

As of now, this proxy only works with HTTP proxy servers.

        Example:

request.proxy('http://127.0.0.1:80').get('https://wtfismyip.com/json').text().then(console.log)

About

☁️🌐 Easy and Powerful HTTP Requestsβ€” Simplified

Topics

Resources

Stars

Watchers

Forks