Socket
Socket
Sign inDemoInstall

httpreq

Package Overview
Dependencies
0
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    httpreq

node-httpreq is a node.js library to do HTTP(S) requests the easy way


Version published
Weekly downloads
981K
decreased by-4.33%
Maintainers
1
Install size
76.0 kB
Created
Weekly downloads
 

Readme

Source

node-httpreq

node-httpreq is a node.js library to do HTTP(S) requests the easy way

Do GET, POST, upload files, use cookies, change headers, ...

Install

You can install httpreq using the Node Package Manager (npm):

npm install httpreq

How to use


### httpreq.get(url, [options], callback)

Arguments

  • url: The url to connect to. Can be http or https.
  • options: (optional) The following options can be passed:
    • parameters: an object of query parameters
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
    • body: custom body content you want to send
    • allowRedirects: (default: true ...only with httpreq.get ), if true, redirects will be followed
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )

Example without options

var httpreq = require('httpreq');

httpreq.get('http://www.google.com', function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(res.headers);
		console.log(res.body);
	}
});

Example with options

var httpreq = require('httpreq');

httpreq.get('http://posttestserver.com/post.php', {
	parameters: {
		name: 'John',
		lastname: 'Doe'
	},
	headers:{
		'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
	},
	cookies: [
		'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
		'id=2'
	]
}, function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(res.body);
	}
});

### httpreq.post(url, [options], callback)

Arguments

  • url: The url to connect to. Can be http or https.
  • options: (optional) The following options can be passed:
    • parameters: an object of post parameters ( application/x-www-form-urlencoded is used)
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
    • body: custom body content you want to send. Parameters are ignored when this is used.
    • allowRedirects: (default: false), if true, redirects will be followed
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )

Example without extra options

var httpreq = require('httpreq');

httpreq.post('http://posttestserver.com/post.php', {
	parameters: {
		name: 'John',
		lastname: 'Doe'
	}
}, function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(res.body);
	}
});

Example with options

var httpreq = require('httpreq');

httpreq.post('http://posttestserver.com/post.php', {
	parameters: {
		name: 'John',
		lastname: 'Doe'
	},
	headers:{
		'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
	},
	cookies: [
		'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
		'id=2'
	]
}, function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(res.body);
	}
});

### httpreq.uploadFiles(options, callback)

Arguments

  • options: The following options can be passed:
    • url: the url to post the files to
    • parameters: an object of post parameters ( multipart/form-data is used)
    • files: an object of files (can be more than one)
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )
var httpreq = require('httpreq');

httpreq.uploadFiles({
	url: "http://rekognition.com/demo/do_upload/",
	parameters:{
		name_space	: 'something',
	},
	files:{
		fileToUpload: __dirname + "/exampleupload.jpg"
	}},
function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(res.body);
	}
});

### httpreq.doRequest(options, callback)

This is used by httpreq.get() and httpreq.post()

Arguments

  • options: The following options can be passed:
    • url: the url to post the files to
    • method: 'GET' or 'POST'
    • parameters: an object of query/post parameters
    • files: an object of files (can be more than one)
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
    • body: custom body content you want to send
    • allowRedirects: (default: false), if true, redirects will be followed
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )

Example

var httpreq = require('httpreq');

httpreq.doRequest({
	url: 'https://graph.facebook.com/19292868552',
	method: 'GET',
	parameters: {
		name: 'test'
	}
},
function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(JSON.parse(res.body));
	}
});

### Downloading a binary file To download a binary file, just add __binary: true__ to the options when doing a get or a post.

Example

var httpreq = require('httpreq');

httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {binary: true}, function (err, res){
    if (err){
        console.log(err);
    }else{
        fs.writeFile(__dirname + '/test.png', res.body, function (err) {
            if(err)
                console.log("error writing file");
        });
    }
});

### Sending a custom body Use the body option to send a custom body (eg. an xml post)

Example

var httpreq = require('httpreq');

httpreq.post('http://posttestserver.com/post.php',{
    body: '<?xml version="1.0" encoding="UTF-8"?>',
    headers:{
        'Content-Type': 'text/xml',
    }},
    function (err, res) {
        if (err){
            console.log(err);
        }else{
            console.log(res.body);
        }
    }
);

FAQs

Last updated on 09 Mar 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc