Skip to content
network request helper
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
examples
lib
tests
.gitignore
.npmignore
LICENSE
Makefile
Readme.md
index.js
package.json

Readme.md

node-request

A simple network request helper that is geared towards crawling

installation

$ npm install git://github.com/icodeforlove/node-request.git

super simple to use

var Request = require('request'),
    request = new Request();

request.get(/* URL */, function (body) {
    console.log(body)
});

request.get(/* URL */, /* REQUEST_OBJECT */, function (body) {
    console.log(body)
});

request initialization

var Request = ('request');

var request = new Request({
    cookies: {},
    headers: {},
    timeout: 4000,
    retries: 3,
    encoding 'utf8',
    // didRequestFail: null, (this has its own section)
    // signRequest: null, (this has its own section)
    dataType: 'raw' // or JSON,
    auth: {username: 'username', password: 'password'} // basic auth for all requests
});

if you initialize the request object with any of the above properties every request will default to those settings, you can over ride them on a per request basis

var options = {
    encoding: 'binary',
    proxy: {ip: '127.0.0.1', port: 1337},
    data: {foo: 'bar'},
    cookies: {foo: 'bar'},
    auth: {username: 'username', password: 'password'} // basic auth for request
};

request.get(/* URL */, options, function (body) {
    console.log(body)
});

request signatures

you can create a custom request signature function like this

var qs = require('querystring');

var request = new Request({
    signRequest: function (data) {
        // do something with the data
        return qs.stringify(data);
    }
});

posting

request.post('http://localhost?something=123', {data: {foo: 'bar', bar: 'foo'}}, function (body) {
    console.log(body)
});

multipart

the multipart request works a little different, in the data object you can prefix a values key with '@' like this

request.multipart('http://localhost?something=123', {data: {'@filename': 'filepath', bar: 'foo'}}, function (body) {
    console.log(body)
});

this will create a multipart request and upload files

Something went wrong with that request. Please try again.