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 = ;var request = ; { // handle request response} ; // 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 = ;var request = ; var user_options = method: 'get' url: 'https://your.api' ; { // handle request response} ; // 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 = ;var request = ; var user_options = method: 'get' timeout: 3000 url: 'https://your.api' ; { // handle request response} ; // 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 = ;var request = ; var request_headers = headers: accept: 'application/json' ; var user_options = method: 'get' url: 'https://your.api' ; { // handle request response} ; // 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 = ;var request = ; { // handle request response} { var user_options = method: 'get' url: 'https://your.api' ; ; ;); // 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'}