node package manager

httpreq

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

node-httpreq

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

Do GET, POST, PUT, PATCH, DELETE, OPTIONS, upload files, use cookies, change headers, ...

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

npm install httpreq
var httpreq = require('httpreq');
 
httpreq.get('http://www.google.com', function (err, res){
    if (err) return console.log(err);
 
    console.log(res.statusCode);
    console.log(res.headers);
    console.log(res.body);
    console.log(res.cookies);
});

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

Arguments

Example without options

var httpreq = require('httpreq');
 
httpreq.get('http://www.google.com', function (err, res){
    if (err) return console.log(err);
 
    console.log(res.statusCode);
    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

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.put(url, [options], callback)

Same options as httpreq.post(url, [options], callback)


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

Same options as httpreq.post(url, [options], callback)


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

Same options as httpreq.get(url, [options], callback) except for the ability to follow redirects.


### Uploading files

You can still use httpreq.uploadFiles({url: 'url', files: {}}, callback), but it's easier to just use POST (or PUT):

Example

var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/upload.php', {
    parameters: {
        name: 'John',
        lastname: 'Doe'
    },
    files:{
        myfile: __dirname + "/testupload.jpg",
        myotherfile: __dirname + "/testupload.jpg"
    }
}, function (err, res){
    if (err) throw err;
});

### 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");
        });
    }
});

### Downloading a file directly to disk To download a file directly to disk, use the download method provided.

Downloading is done using a stream, so the data is not stored in memory and directly saved to file.

Example

var httpreq = require('httpreq');
 
httpreq.download(
    'https://ssl.gstatic.com/gb/images/k1_a31af7ac.png',
    __dirname + '/test.png'
, function (err, progress){
    if (err) return console.log(err);
    console.log(progress);
}, function (err, res){
    if (err) return console.log(err);
    console.log(res);
});
 

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

### Using a http(s) proxy

Example

var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/post.php', {
    proxy: {
        host: '10.100.0.126',
        port: 8888
    }
}, function (err, res){
    if (err){
        console.log(err);
    }else{
        console.log(res.body);
    }
});

### httpreq.doRequest(options, callback)

httpreq.doRequest is internally used by httpreq.get() and httpreq.post(). You can use this directly. Everything is stays the same as httpreq.get() or httpreq.post() except that the following options MUST be passed:

  • url: the url to post the files to
  • method: 'GET', 'POST', 'PUT' or 'DELETE'