/request

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

Primary LanguageJavaScript

*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)