Tiny module for http requests with no dependencies
npm install tiny_request
var req = require('tiny_request')
req.get('http://google.com', function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
To automatically parse JSON you just need to pass JSON parameter.
Example:
req.get({ url: 'http://test.com/json', json: true}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body) //body now is parsed JSON object
}
})
req.get({ url: 'http://test.com', query: { test: 'test' }}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
Where query is GET parameters object
Also you can pass port parameter, for example:
req.get({ url: 'http://test.com', port: 8080}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
var data = {
image: {
value: fs.createReadStream('photo.png'),
filename: 'photo.png',
contentType: 'image/png'
},
test: 'test'
}
req.post({
url: 'http://test.com',
query: { test: 'test' },
multipart: data
}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
var form = {
test: 'test'
}
req.post({ url: 'http://test.com', form: form}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
var data = {
test: 'test'
}
req.post({ url: 'http://test.com', jsonData: data}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
var headers = {
'Test-Header': 'test'
}
req.post({ url: 'http://test.com', headers: headers}, function(body, response, err){
if (!err && response.statusCode == 200) {
console.log(body)
}
})
req.get({url: url, pipe: stream})
req.get({url: url, timeout: 1000})
Request will be aborted after 1 second if there is no response from server