How to use the defaults function from request

Find comprehensive JavaScript request.defaults code examples handpicked from public code repositorys.

request.defaults is a Node.js module that allows you to set default options for HTTP requests made with the request module, which can be overridden on a per-request basis if needed.

51
52
53
54
55
56
57
58
59
60
61
      /<%apiKey%>/g,
      this.c.apiKey
    );
  }
  if ('request' in this.c) {
    request = requestRoot.defaults(this.c.request);
  }


  return this.c;
};
fork icon171
star icon164
watch icon72

25
26
27
28
29
30
31
32
33
34

  };

  this.options.jar = this.jar;

  this.request = Request.defaults(this.options);

}

getUserAgent() {
fork icon35
star icon107
watch icon9

+ 9 other calls in file

How does request.defaults work?

request.defaults works by creating a new request function with default options that can be overridden when making HTTP requests.

When you call request with a URL and any additional options, the request function first merges the default options with the provided options.

This allows you to set up a sensible default configuration for your HTTP requests, but still give users the ability to customize it based on their specific needs.

The default options can include things like headers, query parameters, authentication, timeouts, and other request-related settings.

73
74
75
76
77
78
79
80
81
82
}

export function defaults(options: RequestOptions): void {
    if (options.throwResponseError !== undefined)
        throwResponseError = options.throwResponseError;
    request.defaults(options);
}

export function debug(value?: boolean): boolean {
    if (value === undefined)
fork icon7
star icon36
watch icon4

+ 9 other calls in file

32
33
34
35
36
37
38
39
40
41
42
        if (!err) {
            requestDefaults.proxy = c.httpsProxy || c.httpProxy;
            registry.httpProxy = c.httpProxy;
            registry.httpsProxy = c.httpsProxy || c.httpProxy;
        }
        Request.defaults(requestDefaults);
    });


    return this;
};
fork icon14
star icon33
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const request = require("request");

// Set default options for HTTP requests
const defaults = {
  baseUrl: "https://jsonplaceholder.typicode.com",
  headers: {
    "User-Agent": "MyApp/1.0",
  },
  timeout: 5000,
};

// Create a new request function with default options
const myRequest = request.defaults(defaults);

// Make an HTTP GET request with the default options
myRequest.get("/todos/1", (error, response, body) => {
  if (error) {
    console.error("Error:", error);
  } else {
    console.log("Response:", body);
  }
});

// Override some of the default options for a specific request
myRequest.get(
  {
    url: "/todos/2",
    headers: {
      "X-Custom-Header": "foobar",
    },
  },
  (error, response, body) => {
    if (error) {
      console.error("Error:", error);
    } else {
      console.log("Response:", body);
    }
  }
);

In this example, we first set default options for HTTP requests using request.defaults. We then create a new request function with the default options, which can be used to make HTTP requests just like the regular request function. We demonstrate making a GET request to the JSONPlaceholder API with the default options, and then overriding some of the default options (specifically the headers) for a second GET request to a different endpoint. Both requests are made using the same myRequest function, but the second request uses some different options that override the defaults.

66
67
68
69
70
71
72
73
74
75

  defaults.headers = Object.assign({ 'User-Agent': userAgent }, options.request.headers)

  logger.debug('request options set to %j', defaults)

  connection.request = request.defaults(defaults)
},
/**
 *
 * @author Facundo
fork icon2
star icon8
watch icon3

+ 6 other calls in file

6
7
8
9
10
11
12
13
14
15
const $ = new Env('哈佛智家');
const axios = require('axios');
let CryptoJS = require('crypto-js')
let request = require("request");
const JSEncrypt = require('node-jsencrypt');
request = request.defaults({
    jar: true
});
const {
    log
fork icon2
star icon0
watch icon0

1
2
3
4
5
6
7
8
9
10
const request = require('request'),
    cloudscraper = require('cloudscraper'),
    net = require('net'),
    URL = require('url'),
    requestJar = request.jar(),
    reqCookie = request.defaults({
        jar: requestJar
    }),
    reqBypass = cloudscraper.defaults({
        jar: requestJar
fork icon1
star icon0
watch icon0

31
32
33
34
35
36
37
38
39
40
const domParser = new XmldomParser({
    errorHandler: {}
})
const {JSDOM} = require('jsdom');
let request = require("request");
request = request.defaults({jar: true});
const {log} = console;
const Notify = 1; //0为关闭通知,1为打开通知,默认为1
const debug = 0; //0为关闭调试,1为打开调试,默认为0
//////////////////////
fork icon0
star icon3
watch icon1

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
constructor(options) {
    super();

    this.debug = false;
    this.options = options;
    this.baseRequest = request.defaults({
        pool: { maxSockets: this.options.overrides.request.maxSockets ?? 4 },
        timeout: this.options.overrides.request.timeout ?? 10000
    });
    this.overrides = {
fork icon0
star icon2
watch icon0

0
1
2
3
4
5
6
7
8
9
const _ = require('lodash');
const _fs = require('fs');
const _path = require('path');
const _https = require('https');
const requestLib = require('request');
const _request = requestLib.defaults({jar: requestLib.jar(), strictSSL: false});
const _nodeUtil = require('util');
const moment = require('moment');
const crypto = require('crypto');
const request = require('request');
fork icon0
star icon1
watch icon0

192
193
194
195
196
197
198
199
200
201
    filesArray = fileList;
}

var downloaded = 0;

var baseRequest = request.defaults({
    pool: {
        maxSockets: 10
    },
    time: 100000
fork icon4
star icon2
watch icon3

93
94
95
96
97
98
99
100
101
102
103
104
var ANDROID_SAFE_FILENAME = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-+,@£$€!½§~\'=()[]{}0123456789';


//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json
BigInt.prototype.toJSON = function() { return this.toString() };


var baseRequest = request.defaults(cfgRequestDefaults);
let outboxUrlExclusionRegex = null;
if ("" !== cfgTokenOutboxUrlExclusionRegex) {
  outboxUrlExclusionRegex = new RegExp(cfgTokenOutboxUrlExclusionRegex);
}
fork icon0
star icon0
watch icon1

+ 4 other calls in file

22
23
24
25
26
27
28
29
30
31
    this.refreshNext = false;
    this.appName = "Southernrail_Interface";
    this.appVersion = "0.0.4";
};

#customHeaderRequest = request.defaults({
    headers: { 'User-Agent': `${this.appName}/${this.appVersion} (NodeJS_${process.version}) ${os.platform()} (${os.arch()}) NodeJS Wrapper` }
})

/**
fork icon0
star icon0
watch icon2

104
105
106
107
108
109
110
111
112
113
options.body = req;
delete options.protocol;
options.uri = ctx.fullUrl;
let r = request;
if (opts.proxyUrl) {
  r = request.defaults({ proxy: opts.proxyUrl });
} else if (opts.host || options.port > 0) {
  const uri = urlParse(ctx.fullUrl);
  options.uri = uri;
  if (opts.host) {
fork icon0
star icon0
watch icon1

223
224
225
226
227
228
229
230
231
232
233
234
    this._settings.key = this._settings.clientKey;
    this._settings.cert = this._settings.clientCert;
  }


  debug('RestConnector:this._settings', this._settings);
  this._request = request.defaults(this._settings);
}


/**
 * Hook for defining a model by the data source
fork icon0
star icon0
watch icon0