generic-request-options
v1.2.4
Published
provides a generic set of request options for the request package
Downloads
6
Maintainers
Readme
generic request options
provides a generic set of request options for the request package that can be augmented by providing user_options
and request_headers
.
table of contents
installation
npm install generic-request-options
use
getGenericRequestOptions( [user_options][, request_headers] )
@param {Object} [user_options]
@param {Object} [request_headers]
@returns {Object}
user_options
any valid request options
request_headers
the intention is to pass in optional http request headers that will be used by the request method.
in order to pass on proxy headers, you can also pass in the req.headers
that make up the header object of an express route request. the intent being to pass on the proxy headers:
- x-forwarded-for
- x-forwarded-proto
- x-real-agent
- x-real-ip
basic
the basic use case will return the http headers date and user-agent, and a timeout
var getGenericRequestOptions = require( 'generic-request-options' );
var request = require( 'request' );
function responseHandler( err, res, body ) {
// handle request response
}
request( getGenericRequestOptions(), responseHandler );
// getGenericRequestOptions => {
headers: {
date: '<new Date().toUTCString()>'
'user-agent': 'node.js/<node-version> request (https://www.npmjs.com/package/request)'
},
timeout: 10000
}
adding user options
var getGenericRequestOptions = require( 'generic-request-options' );
var request = require( 'request' );
var user_options = {
method: 'get',
url: 'https://your.api'
};
function responseHandler( err, res, body ) {
// handle request response
}
request( getGenericRequestOptions( user_options ), responseHandler );
// getGenericRequestOptions => {
headers: {
date: '<new Date().toUTCString()>'
'user-agent': 'node.js/<node-version> request (https://www.npmjs.com/package/request)'
},
method: 'get',
timeout: 10000,
url: 'https://your.api'
}
overriding the default timeout
var getGenericRequestOptions = require( 'generic-request-options' );
var request = require( 'request' );
var user_options = {
method: 'get',
timeout: 3000,
url: 'https://your.api'
};
function responseHandler( err, res, body ) {
// handle request response
}
request( getGenericRequestOptions( user_options ), responseHandler );
// getGenericRequestOptions => {
headers: {
date: '<new Date().toUTCString()>'
'user-agent': 'node.js/<node-version> request (https://www.npmjs.com/package/request)'
},
method: 'get',
timeout: 3000,
url: 'https://your.api'
}
adding additional headers
var getGenericRequestOptions = require( 'generic-request-options' );
var request = require( 'request' );
var request_headers = {
headers: {
accept: 'application/json'
}
};
var user_options = {
method: 'get',
url: 'https://your.api'
};
function responseHandler( err, res, body ) {
// handle request response
}
request( getGenericRequestOptions( user_options, request_headers ), responseHandler );
// getGenericRequestOptions => {
headers: {
accept: 'application/json',
date: '<new Date().toUTCString()>',
'user-agent': 'node.js/<node-version> request (https://www.npmjs.com/package/request)'
},
method: 'get',
timeout: 10000,
url: 'https://your.api'
}
with req.headers
where req.headers
is, for example, the header object from an express route request
var getGenericRequestOptions = require( 'generic-request-options' );
var request = require( 'request' );
function responseHandler( err, res, body ) {
// handle request response
}
function middleware( req, res, next ) {
var user_options = {
method: 'get',
url: 'https://your.api'
};
request( getGenericRequestOptions( user_options, req.headers ), responseHandler );
next();
);
// getGenericRequestOptions => {
headers: {
date: '<new Date().toUTCString()>'
'user-agent': 'node.js/<node-version> request (https://www.npmjs.com/package/request)'
'x-forwarded-for': <request_headers['x-forward-for']>,
'x-forwarded-proto': <request_headers['x-forward-for']>,
'x-real-agent': <request_headers['x-real-agent']>,
'x-real-ip': <request_headers['x-real-ip']>
},
method: 'get',
timeout: 10000,
url: 'https://your.api'
}