Skip to content

Naltox/tiny_request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tiny module for http requests with no dependencies

Install

npm install tiny_request

Simple to use

var req = require('tiny_request')

req.get('http://google.com', function(body, response, err){
	if (!err && response.statusCode == 200) {  
		console.log(body) 
	} 
})

Examples

JSON

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

GET

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

POST Multipart

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

POST Forms

var form = {
	test: 'test'
}

req.post({ url: 'http://test.com', form: form}, function(body, response, err){
	if (!err && response.statusCode == 200) {  
		console.log(body) 
	} 
})

POST Json

var data = {
	test: 'test'
}

req.post({ url: 'http://test.com', jsonData: data}, function(body, response, err){
	if (!err && response.statusCode == 200) {  
		console.log(body) 
	} 
})

Custom HTTP Headers

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

Stream

req.get({url: url, pipe: stream}) 

Timeout

req.get({url: url, timeout: 1000})

Request will be aborted after 1 second if there is no response from server

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published