*Note: At this time, the module is not named, therefore, the name request
is used to represent this module.
βοΈπ Easy and Powerful HTTP Requestsβ Simplified
// 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()
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
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
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)
})
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
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.
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)