Skip to content

Instantly share code, notes, and snippets.

@johntitus
Created November 10, 2016 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntitus/e762d7f60d855999a31133fd52d30e0f to your computer and use it in GitHub Desktop.
Save johntitus/e762d7f60d855999a31133fd52d30e0f to your computer and use it in GitHub Desktop.
How to use the Node.js Request module with defaults
var request = require('request').defaults({
headers: {
'X-header1': myvar,
'Content-Type': 'application/json'
}
});
// Now request will automatically send the headers specified above on every request.
request.get('http://www.example.com', function(err, resp, body){
//...
});
// You can also do
var otherTypeOfDefaultRequest = require('request').defaults({
headers: {
'Content-Type': 'text/xml'
}
});
// Will automatically send the headers specified above.
otherTypeOfDefaultRequest.get('http://www.example.com', function(err, resp, body){
//...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment