provides a generic set of request options for the request package that can be augmented by providing user_options
and request_headers
.
npm install generic-request-options
@param {Object} [user_options]
@param {Object} [request_headers]
@returns {Object}
any valid request options
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
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
}
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'
}
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'
}
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'
}
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'
}