xref: /third_party/node/doc/api/https.md (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci# HTTPS
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/https.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciHTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a
101cb0ef41Sopenharmony_ciseparate module.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci## Determining if crypto support is unavailable
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciIt is possible for Node.js to be built without including support for the
151cb0ef41Sopenharmony_ci`node:crypto` module. In such cases, attempting to `import` from `https` or
161cb0ef41Sopenharmony_cicalling `require('node:https')` will result in an error being thrown.
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciWhen using CommonJS, the error thrown can be caught using try/catch:
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci<!-- eslint-skip -->
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci```cjs
231cb0ef41Sopenharmony_cilet https;
241cb0ef41Sopenharmony_citry {
251cb0ef41Sopenharmony_ci  https = require('node:https');
261cb0ef41Sopenharmony_ci} catch (err) {
271cb0ef41Sopenharmony_ci  console.error('https support is disabled!');
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci```
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciWhen using the lexical ESM `import` keyword, the error can only be
321cb0ef41Sopenharmony_cicaught if a handler for `process.on('uncaughtException')` is registered
331cb0ef41Sopenharmony_ci_before_ any attempt to load the module is made (using, for instance,
341cb0ef41Sopenharmony_cia preload module).
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciWhen using ESM, if there is a chance that the code may be run on a build
371cb0ef41Sopenharmony_ciof Node.js where crypto support is not enabled, consider using the
381cb0ef41Sopenharmony_ci[`import()`][] function instead of the lexical `import` keyword:
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci```mjs
411cb0ef41Sopenharmony_cilet https;
421cb0ef41Sopenharmony_citry {
431cb0ef41Sopenharmony_ci  https = await import('node:https');
441cb0ef41Sopenharmony_ci} catch (err) {
451cb0ef41Sopenharmony_ci  console.error('https support is disabled!');
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci```
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci## Class: `https.Agent`
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci<!-- YAML
521cb0ef41Sopenharmony_ciadded: v0.4.5
531cb0ef41Sopenharmony_cichanges:
541cb0ef41Sopenharmony_ci  - version: v5.3.0
551cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/4252
561cb0ef41Sopenharmony_ci    description: support `0` `maxCachedSessions` to disable TLS session caching.
571cb0ef41Sopenharmony_ci  - version: v2.5.0
581cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/2228
591cb0ef41Sopenharmony_ci    description: parameter `maxCachedSessions` added to `options` for TLS
601cb0ef41Sopenharmony_ci                 sessions reuse.
611cb0ef41Sopenharmony_ci-->
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciAn [`Agent`][] object for HTTPS similar to [`http.Agent`][]. See
641cb0ef41Sopenharmony_ci[`https.request()`][] for more information.
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci### `new Agent([options])`
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci<!-- YAML
691cb0ef41Sopenharmony_cichanges:
701cb0ef41Sopenharmony_ci  - version: v12.5.0
711cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/28209
721cb0ef41Sopenharmony_ci    description: do not automatically set servername if the target host was
731cb0ef41Sopenharmony_ci                 specified using an IP address.
741cb0ef41Sopenharmony_ci-->
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci* `options` {Object} Set of configurable options to set on the agent.
771cb0ef41Sopenharmony_ci  Can have the same fields as for [`http.Agent(options)`][], and
781cb0ef41Sopenharmony_ci  * `maxCachedSessions` {number} maximum number of TLS cached sessions.
791cb0ef41Sopenharmony_ci    Use `0` to disable TLS session caching. **Default:** `100`.
801cb0ef41Sopenharmony_ci  * `servername` {string} the value of
811cb0ef41Sopenharmony_ci    [Server Name Indication extension][sni wiki] to be sent to the server. Use
821cb0ef41Sopenharmony_ci    empty string `''` to disable sending the extension.
831cb0ef41Sopenharmony_ci    **Default:** host name of the target server, unless the target server
841cb0ef41Sopenharmony_ci    is specified using an IP address, in which case the default is `''` (no
851cb0ef41Sopenharmony_ci    extension).
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci    See [`Session Resumption`][] for information about TLS session reuse.
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci#### Event: `'keylog'`
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci<!-- YAML
921cb0ef41Sopenharmony_ciadded:
931cb0ef41Sopenharmony_ci - v13.2.0
941cb0ef41Sopenharmony_ci - v12.16.0
951cb0ef41Sopenharmony_ci-->
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci* `line` {Buffer} Line of ASCII text, in NSS `SSLKEYLOGFILE` format.
981cb0ef41Sopenharmony_ci* `tlsSocket` {tls.TLSSocket} The `tls.TLSSocket` instance on which it was
991cb0ef41Sopenharmony_ci  generated.
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciThe `keylog` event is emitted when key material is generated or received by a
1021cb0ef41Sopenharmony_ciconnection managed by this agent (typically before handshake has completed, but
1031cb0ef41Sopenharmony_cinot necessarily). This keying material can be stored for debugging, as it
1041cb0ef41Sopenharmony_ciallows captured TLS traffic to be decrypted. It may be emitted multiple times
1051cb0ef41Sopenharmony_cifor each socket.
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciA typical use case is to append received lines to a common text file, which is
1081cb0ef41Sopenharmony_cilater used by software (such as Wireshark) to decrypt the traffic:
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci```js
1111cb0ef41Sopenharmony_ci// ...
1121cb0ef41Sopenharmony_cihttps.globalAgent.on('keylog', (line, tlsSocket) => {
1131cb0ef41Sopenharmony_ci  fs.appendFileSync('/tmp/ssl-keys.log', line, { mode: 0o600 });
1141cb0ef41Sopenharmony_ci});
1151cb0ef41Sopenharmony_ci```
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci## Class: `https.Server`
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci<!-- YAML
1201cb0ef41Sopenharmony_ciadded: v0.3.4
1211cb0ef41Sopenharmony_ci-->
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci* Extends: {tls.Server}
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciSee [`http.Server`][] for more information.
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci### `server.close([callback])`
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci<!-- YAML
1301cb0ef41Sopenharmony_ciadded: v0.1.90
1311cb0ef41Sopenharmony_ci-->
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci* `callback` {Function}
1341cb0ef41Sopenharmony_ci* Returns: {https.Server}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciSee [`server.close()`][] in the `node:http` module.
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci### `server.closeAllConnections()`
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci<!-- YAML
1411cb0ef41Sopenharmony_ciadded: v18.2.0
1421cb0ef41Sopenharmony_ci-->
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ciSee [`server.closeAllConnections()`][] in the `node:http` module.
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci### `server.closeIdleConnections()`
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci<!-- YAML
1491cb0ef41Sopenharmony_ciadded: v18.2.0
1501cb0ef41Sopenharmony_ci-->
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ciSee [`server.closeIdleConnections()`][] in the `node:http` module.
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci### `server.headersTimeout`
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci<!-- YAML
1571cb0ef41Sopenharmony_ciadded: v11.3.0
1581cb0ef41Sopenharmony_ci-->
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci* {number} **Default:** `60000`
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciSee [`server.headersTimeout`][] in the `node:http` module.
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci### `server.listen()`
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ciStarts the HTTPS server listening for encrypted connections.
1671cb0ef41Sopenharmony_ciThis method is identical to [`server.listen()`][] from [`net.Server`][].
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci### `server.maxHeadersCount`
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci* {number} **Default:** `2000`
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ciSee [`server.maxHeadersCount`][] in the `node:http` module.
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci### `server.requestTimeout`
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci<!-- YAML
1781cb0ef41Sopenharmony_ciadded: v14.11.0
1791cb0ef41Sopenharmony_cichanges:
1801cb0ef41Sopenharmony_ci  - version: v18.0.0
1811cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41263
1821cb0ef41Sopenharmony_ci    description: The default request timeout changed
1831cb0ef41Sopenharmony_ci                 from no timeout to 300s (5 minutes).
1841cb0ef41Sopenharmony_ci-->
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci* {number} **Default:** `300000`
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ciSee [`server.requestTimeout`][] in the `node:http` module.
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci### `server.setTimeout([msecs][, callback])`
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci<!-- YAML
1931cb0ef41Sopenharmony_ciadded: v0.11.2
1941cb0ef41Sopenharmony_ci-->
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci* `msecs` {number} **Default:** `120000` (2 minutes)
1971cb0ef41Sopenharmony_ci* `callback` {Function}
1981cb0ef41Sopenharmony_ci* Returns: {https.Server}
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ciSee [`server.setTimeout()`][] in the `node:http` module.
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci### `server.timeout`
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci<!-- YAML
2051cb0ef41Sopenharmony_ciadded: v0.11.2
2061cb0ef41Sopenharmony_cichanges:
2071cb0ef41Sopenharmony_ci  - version: v13.0.0
2081cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/27558
2091cb0ef41Sopenharmony_ci    description: The default timeout changed from 120s to 0 (no timeout).
2101cb0ef41Sopenharmony_ci-->
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci* {number} **Default:** 0 (no timeout)
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ciSee [`server.timeout`][] in the `node:http` module.
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci### `server.keepAliveTimeout`
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci<!-- YAML
2191cb0ef41Sopenharmony_ciadded: v8.0.0
2201cb0ef41Sopenharmony_ci-->
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci* {number} **Default:** `5000` (5 seconds)
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ciSee [`server.keepAliveTimeout`][] in the `node:http` module.
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci## `https.createServer([options][, requestListener])`
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci<!-- YAML
2291cb0ef41Sopenharmony_ciadded: v0.3.4
2301cb0ef41Sopenharmony_ci-->
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci* `options` {Object} Accepts `options` from [`tls.createServer()`][],
2331cb0ef41Sopenharmony_ci  [`tls.createSecureContext()`][] and [`http.createServer()`][].
2341cb0ef41Sopenharmony_ci* `requestListener` {Function} A listener to be added to the `'request'` event.
2351cb0ef41Sopenharmony_ci* Returns: {https.Server}
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci```js
2381cb0ef41Sopenharmony_ci// curl -k https://localhost:8000/
2391cb0ef41Sopenharmony_ciconst https = require('node:https');
2401cb0ef41Sopenharmony_ciconst fs = require('node:fs');
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ciconst options = {
2431cb0ef41Sopenharmony_ci  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
2441cb0ef41Sopenharmony_ci  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
2451cb0ef41Sopenharmony_ci};
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_cihttps.createServer(options, (req, res) => {
2481cb0ef41Sopenharmony_ci  res.writeHead(200);
2491cb0ef41Sopenharmony_ci  res.end('hello world\n');
2501cb0ef41Sopenharmony_ci}).listen(8000);
2511cb0ef41Sopenharmony_ci```
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciOr
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci```js
2561cb0ef41Sopenharmony_ciconst https = require('node:https');
2571cb0ef41Sopenharmony_ciconst fs = require('node:fs');
2581cb0ef41Sopenharmony_ci
2591cb0ef41Sopenharmony_ciconst options = {
2601cb0ef41Sopenharmony_ci  pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),
2611cb0ef41Sopenharmony_ci  passphrase: 'sample',
2621cb0ef41Sopenharmony_ci};
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_cihttps.createServer(options, (req, res) => {
2651cb0ef41Sopenharmony_ci  res.writeHead(200);
2661cb0ef41Sopenharmony_ci  res.end('hello world\n');
2671cb0ef41Sopenharmony_ci}).listen(8000);
2681cb0ef41Sopenharmony_ci```
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci## `https.get(options[, callback])`
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci## `https.get(url[, options][, callback])`
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci<!-- YAML
2751cb0ef41Sopenharmony_ciadded: v0.3.6
2761cb0ef41Sopenharmony_cichanges:
2771cb0ef41Sopenharmony_ci  - version: v10.9.0
2781cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/21616
2791cb0ef41Sopenharmony_ci    description: The `url` parameter can now be passed along with a separate
2801cb0ef41Sopenharmony_ci                 `options` object.
2811cb0ef41Sopenharmony_ci  - version: v7.5.0
2821cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10638
2831cb0ef41Sopenharmony_ci    description: The `options` parameter can be a WHATWG `URL` object.
2841cb0ef41Sopenharmony_ci-->
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci* `url` {string | URL}
2871cb0ef41Sopenharmony_ci* `options` {Object | string | URL} Accepts the same `options` as
2881cb0ef41Sopenharmony_ci  [`https.request()`][], with the method set to GET by default.
2891cb0ef41Sopenharmony_ci* `callback` {Function}
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ciLike [`http.get()`][] but for HTTPS.
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci`options` can be an object, a string, or a [`URL`][] object. If `options` is a
2941cb0ef41Sopenharmony_cistring, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
2951cb0ef41Sopenharmony_ciobject, it will be automatically converted to an ordinary `options` object.
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci```js
2981cb0ef41Sopenharmony_ciconst https = require('node:https');
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_cihttps.get('https://encrypted.google.com/', (res) => {
3011cb0ef41Sopenharmony_ci  console.log('statusCode:', res.statusCode);
3021cb0ef41Sopenharmony_ci  console.log('headers:', res.headers);
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci  res.on('data', (d) => {
3051cb0ef41Sopenharmony_ci    process.stdout.write(d);
3061cb0ef41Sopenharmony_ci  });
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci}).on('error', (e) => {
3091cb0ef41Sopenharmony_ci  console.error(e);
3101cb0ef41Sopenharmony_ci});
3111cb0ef41Sopenharmony_ci```
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ci## `https.globalAgent`
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ci<!-- YAML
3161cb0ef41Sopenharmony_ciadded: v0.5.9
3171cb0ef41Sopenharmony_ci-->
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ciGlobal instance of [`https.Agent`][] for all HTTPS client requests.
3201cb0ef41Sopenharmony_ci
3211cb0ef41Sopenharmony_ci## `https.request(options[, callback])`
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci## `https.request(url[, options][, callback])`
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci<!-- YAML
3261cb0ef41Sopenharmony_ciadded: v0.3.6
3271cb0ef41Sopenharmony_cichanges:
3281cb0ef41Sopenharmony_ci  - version:
3291cb0ef41Sopenharmony_ci      - v16.7.0
3301cb0ef41Sopenharmony_ci      - v14.18.0
3311cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/39310
3321cb0ef41Sopenharmony_ci    description: When using a `URL` object parsed username
3331cb0ef41Sopenharmony_ci                 and password will now be properly URI decoded.
3341cb0ef41Sopenharmony_ci  - version:
3351cb0ef41Sopenharmony_ci      - v14.1.0
3361cb0ef41Sopenharmony_ci      - v13.14.0
3371cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/32786
3381cb0ef41Sopenharmony_ci    description: The `highWaterMark` option is accepted now.
3391cb0ef41Sopenharmony_ci  - version: v10.9.0
3401cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/21616
3411cb0ef41Sopenharmony_ci    description: The `url` parameter can now be passed along with a separate
3421cb0ef41Sopenharmony_ci                 `options` object.
3431cb0ef41Sopenharmony_ci  - version: v9.3.0
3441cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/14903
3451cb0ef41Sopenharmony_ci    description: The `options` parameter can now include `clientCertEngine`.
3461cb0ef41Sopenharmony_ci  - version: v7.5.0
3471cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10638
3481cb0ef41Sopenharmony_ci    description: The `options` parameter can be a WHATWG `URL` object.
3491cb0ef41Sopenharmony_ci-->
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci* `url` {string | URL}
3521cb0ef41Sopenharmony_ci* `options` {Object | string | URL} Accepts all `options` from
3531cb0ef41Sopenharmony_ci  [`http.request()`][], with some differences in default values:
3541cb0ef41Sopenharmony_ci  * `protocol` **Default:** `'https:'`
3551cb0ef41Sopenharmony_ci  * `port` **Default:** `443`
3561cb0ef41Sopenharmony_ci  * `agent` **Default:** `https.globalAgent`
3571cb0ef41Sopenharmony_ci* `callback` {Function}
3581cb0ef41Sopenharmony_ci* Returns: {http.ClientRequest}
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ciMakes a request to a secure web server.
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ciThe following additional `options` from [`tls.connect()`][] are also accepted:
3631cb0ef41Sopenharmony_ci`ca`, `cert`, `ciphers`, `clientCertEngine`, `crl`, `dhparam`, `ecdhCurve`,
3641cb0ef41Sopenharmony_ci`honorCipherOrder`, `key`, `passphrase`, `pfx`, `rejectUnauthorized`,
3651cb0ef41Sopenharmony_ci`secureOptions`, `secureProtocol`, `servername`, `sessionIdContext`,
3661cb0ef41Sopenharmony_ci`highWaterMark`.
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci`options` can be an object, a string, or a [`URL`][] object. If `options` is a
3691cb0ef41Sopenharmony_cistring, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
3701cb0ef41Sopenharmony_ciobject, it will be automatically converted to an ordinary `options` object.
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ci`https.request()` returns an instance of the [`http.ClientRequest`][]
3731cb0ef41Sopenharmony_ciclass. The `ClientRequest` instance is a writable stream. If one needs to
3741cb0ef41Sopenharmony_ciupload a file with a POST request, then write to the `ClientRequest` object.
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci```js
3771cb0ef41Sopenharmony_ciconst https = require('node:https');
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ciconst options = {
3801cb0ef41Sopenharmony_ci  hostname: 'encrypted.google.com',
3811cb0ef41Sopenharmony_ci  port: 443,
3821cb0ef41Sopenharmony_ci  path: '/',
3831cb0ef41Sopenharmony_ci  method: 'GET',
3841cb0ef41Sopenharmony_ci};
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ciconst req = https.request(options, (res) => {
3871cb0ef41Sopenharmony_ci  console.log('statusCode:', res.statusCode);
3881cb0ef41Sopenharmony_ci  console.log('headers:', res.headers);
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci  res.on('data', (d) => {
3911cb0ef41Sopenharmony_ci    process.stdout.write(d);
3921cb0ef41Sopenharmony_ci  });
3931cb0ef41Sopenharmony_ci});
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_cireq.on('error', (e) => {
3961cb0ef41Sopenharmony_ci  console.error(e);
3971cb0ef41Sopenharmony_ci});
3981cb0ef41Sopenharmony_cireq.end();
3991cb0ef41Sopenharmony_ci```
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ciExample using options from [`tls.connect()`][]:
4021cb0ef41Sopenharmony_ci
4031cb0ef41Sopenharmony_ci```js
4041cb0ef41Sopenharmony_ciconst options = {
4051cb0ef41Sopenharmony_ci  hostname: 'encrypted.google.com',
4061cb0ef41Sopenharmony_ci  port: 443,
4071cb0ef41Sopenharmony_ci  path: '/',
4081cb0ef41Sopenharmony_ci  method: 'GET',
4091cb0ef41Sopenharmony_ci  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
4101cb0ef41Sopenharmony_ci  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
4111cb0ef41Sopenharmony_ci};
4121cb0ef41Sopenharmony_cioptions.agent = new https.Agent(options);
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ciconst req = https.request(options, (res) => {
4151cb0ef41Sopenharmony_ci  // ...
4161cb0ef41Sopenharmony_ci});
4171cb0ef41Sopenharmony_ci```
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ciAlternatively, opt out of connection pooling by not using an [`Agent`][].
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ci```js
4221cb0ef41Sopenharmony_ciconst options = {
4231cb0ef41Sopenharmony_ci  hostname: 'encrypted.google.com',
4241cb0ef41Sopenharmony_ci  port: 443,
4251cb0ef41Sopenharmony_ci  path: '/',
4261cb0ef41Sopenharmony_ci  method: 'GET',
4271cb0ef41Sopenharmony_ci  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
4281cb0ef41Sopenharmony_ci  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
4291cb0ef41Sopenharmony_ci  agent: false,
4301cb0ef41Sopenharmony_ci};
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ciconst req = https.request(options, (res) => {
4331cb0ef41Sopenharmony_ci  // ...
4341cb0ef41Sopenharmony_ci});
4351cb0ef41Sopenharmony_ci```
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ciExample using a [`URL`][] as `options`:
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ci```js
4401cb0ef41Sopenharmony_ciconst options = new URL('https://abc:xyz@example.com');
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ciconst req = https.request(options, (res) => {
4431cb0ef41Sopenharmony_ci  // ...
4441cb0ef41Sopenharmony_ci});
4451cb0ef41Sopenharmony_ci```
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ciExample pinning on certificate fingerprint, or the public key (similar to
4481cb0ef41Sopenharmony_ci`pin-sha256`):
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ci```js
4511cb0ef41Sopenharmony_ciconst tls = require('node:tls');
4521cb0ef41Sopenharmony_ciconst https = require('node:https');
4531cb0ef41Sopenharmony_ciconst crypto = require('node:crypto');
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_cifunction sha256(s) {
4561cb0ef41Sopenharmony_ci  return crypto.createHash('sha256').update(s).digest('base64');
4571cb0ef41Sopenharmony_ci}
4581cb0ef41Sopenharmony_ciconst options = {
4591cb0ef41Sopenharmony_ci  hostname: 'github.com',
4601cb0ef41Sopenharmony_ci  port: 443,
4611cb0ef41Sopenharmony_ci  path: '/',
4621cb0ef41Sopenharmony_ci  method: 'GET',
4631cb0ef41Sopenharmony_ci  checkServerIdentity: function(host, cert) {
4641cb0ef41Sopenharmony_ci    // Make sure the certificate is issued to the host we are connected to
4651cb0ef41Sopenharmony_ci    const err = tls.checkServerIdentity(host, cert);
4661cb0ef41Sopenharmony_ci    if (err) {
4671cb0ef41Sopenharmony_ci      return err;
4681cb0ef41Sopenharmony_ci    }
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci    // Pin the public key, similar to HPKP pin-sha256 pinning
4711cb0ef41Sopenharmony_ci    const pubkey256 = 'pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=';
4721cb0ef41Sopenharmony_ci    if (sha256(cert.pubkey) !== pubkey256) {
4731cb0ef41Sopenharmony_ci      const msg = 'Certificate verification error: ' +
4741cb0ef41Sopenharmony_ci        `The public key of '${cert.subject.CN}' ` +
4751cb0ef41Sopenharmony_ci        'does not match our pinned fingerprint';
4761cb0ef41Sopenharmony_ci      return new Error(msg);
4771cb0ef41Sopenharmony_ci    }
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ci    // Pin the exact certificate, rather than the pub key
4801cb0ef41Sopenharmony_ci    const cert256 = '25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:' +
4811cb0ef41Sopenharmony_ci      'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16';
4821cb0ef41Sopenharmony_ci    if (cert.fingerprint256 !== cert256) {
4831cb0ef41Sopenharmony_ci      const msg = 'Certificate verification error: ' +
4841cb0ef41Sopenharmony_ci        `The certificate of '${cert.subject.CN}' ` +
4851cb0ef41Sopenharmony_ci        'does not match our pinned fingerprint';
4861cb0ef41Sopenharmony_ci      return new Error(msg);
4871cb0ef41Sopenharmony_ci    }
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ci    // This loop is informational only.
4901cb0ef41Sopenharmony_ci    // Print the certificate and public key fingerprints of all certs in the
4911cb0ef41Sopenharmony_ci    // chain. Its common to pin the public key of the issuer on the public
4921cb0ef41Sopenharmony_ci    // internet, while pinning the public key of the service in sensitive
4931cb0ef41Sopenharmony_ci    // environments.
4941cb0ef41Sopenharmony_ci    do {
4951cb0ef41Sopenharmony_ci      console.log('Subject Common Name:', cert.subject.CN);
4961cb0ef41Sopenharmony_ci      console.log('  Certificate SHA256 fingerprint:', cert.fingerprint256);
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci      hash = crypto.createHash('sha256');
4991cb0ef41Sopenharmony_ci      console.log('  Public key ping-sha256:', sha256(cert.pubkey));
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ci      lastprint256 = cert.fingerprint256;
5021cb0ef41Sopenharmony_ci      cert = cert.issuerCertificate;
5031cb0ef41Sopenharmony_ci    } while (cert.fingerprint256 !== lastprint256);
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci  },
5061cb0ef41Sopenharmony_ci};
5071cb0ef41Sopenharmony_ci
5081cb0ef41Sopenharmony_cioptions.agent = new https.Agent(options);
5091cb0ef41Sopenharmony_ciconst req = https.request(options, (res) => {
5101cb0ef41Sopenharmony_ci  console.log('All OK. Server matched our pinned cert or public key');
5111cb0ef41Sopenharmony_ci  console.log('statusCode:', res.statusCode);
5121cb0ef41Sopenharmony_ci  // Print the HPKP values
5131cb0ef41Sopenharmony_ci  console.log('headers:', res.headers['public-key-pins']);
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci  res.on('data', (d) => {});
5161cb0ef41Sopenharmony_ci});
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_cireq.on('error', (e) => {
5191cb0ef41Sopenharmony_ci  console.error(e.message);
5201cb0ef41Sopenharmony_ci});
5211cb0ef41Sopenharmony_cireq.end();
5221cb0ef41Sopenharmony_ci```
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ciOutputs for example:
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_ci```text
5271cb0ef41Sopenharmony_ciSubject Common Name: github.com
5281cb0ef41Sopenharmony_ci  Certificate SHA256 fingerprint: 25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16
5291cb0ef41Sopenharmony_ci  Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=
5301cb0ef41Sopenharmony_ciSubject Common Name: DigiCert SHA2 Extended Validation Server CA
5311cb0ef41Sopenharmony_ci  Certificate SHA256 fingerprint: 40:3E:06:2A:26:53:05:91:13:28:5B:AF:80:A0:D4:AE:42:2C:84:8C:9F:78:FA:D0:1F:C9:4B:C5:B8:7F:EF:1A
5321cb0ef41Sopenharmony_ci  Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=
5331cb0ef41Sopenharmony_ciSubject Common Name: DigiCert High Assurance EV Root CA
5341cb0ef41Sopenharmony_ci  Certificate SHA256 fingerprint: 74:31:E5:F4:C3:C1:CE:46:90:77:4F:0B:61:E0:54:40:88:3B:A9:A0:1E:D0:0B:A6:AB:D7:80:6E:D3:B1:18:CF
5351cb0ef41Sopenharmony_ci  Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=
5361cb0ef41Sopenharmony_ciAll OK. Server matched our pinned cert or public key
5371cb0ef41Sopenharmony_cistatusCode: 200
5381cb0ef41Sopenharmony_ciheaders: max-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho="; pin-sha256="k2v657xBsOVe1PQRwOsHsw3bsGT2VzIqz5K+59sNQws="; pin-sha256="K87oWBWM9UZfyddvDfoxL+8lpNyoUB2ptGtn0fv6G2Q="; pin-sha256="IQBnNBEiFuhj+8x6X8XLgh01V9Ic5/V3IRQLNFFc7v4="; pin-sha256="iie1VXtL7HzAMF+/PVPR9xzT80kQxdZeJ+zduCB3uj0="; pin-sha256="LvRiGEjRqfzurezaWuj8Wie2gyHMrW5Q06LspMnox7A="; includeSubDomains
5391cb0ef41Sopenharmony_ci```
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci[`Agent`]: #class-httpsagent
5421cb0ef41Sopenharmony_ci[`Session Resumption`]: tls.md#session-resumption
5431cb0ef41Sopenharmony_ci[`URL`]: url.md#the-whatwg-url-api
5441cb0ef41Sopenharmony_ci[`http.Agent(options)`]: http.md#new-agentoptions
5451cb0ef41Sopenharmony_ci[`http.Agent`]: http.md#class-httpagent
5461cb0ef41Sopenharmony_ci[`http.ClientRequest`]: http.md#class-httpclientrequest
5471cb0ef41Sopenharmony_ci[`http.Server`]: http.md#class-httpserver
5481cb0ef41Sopenharmony_ci[`http.createServer()`]: http.md#httpcreateserveroptions-requestlistener
5491cb0ef41Sopenharmony_ci[`http.get()`]: http.md#httpgetoptions-callback
5501cb0ef41Sopenharmony_ci[`http.request()`]: http.md#httprequestoptions-callback
5511cb0ef41Sopenharmony_ci[`https.Agent`]: #class-httpsagent
5521cb0ef41Sopenharmony_ci[`https.request()`]: #httpsrequestoptions-callback
5531cb0ef41Sopenharmony_ci[`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
5541cb0ef41Sopenharmony_ci[`net.Server`]: net.md#class-netserver
5551cb0ef41Sopenharmony_ci[`new URL()`]: url.md#new-urlinput-base
5561cb0ef41Sopenharmony_ci[`server.close()`]: http.md#serverclosecallback
5571cb0ef41Sopenharmony_ci[`server.closeAllConnections()`]: http.md#servercloseallconnections
5581cb0ef41Sopenharmony_ci[`server.closeIdleConnections()`]: http.md#servercloseidleconnections
5591cb0ef41Sopenharmony_ci[`server.headersTimeout`]: http.md#serverheaderstimeout
5601cb0ef41Sopenharmony_ci[`server.keepAliveTimeout`]: http.md#serverkeepalivetimeout
5611cb0ef41Sopenharmony_ci[`server.listen()`]: net.md#serverlisten
5621cb0ef41Sopenharmony_ci[`server.maxHeadersCount`]: http.md#servermaxheaderscount
5631cb0ef41Sopenharmony_ci[`server.requestTimeout`]: http.md#serverrequesttimeout
5641cb0ef41Sopenharmony_ci[`server.setTimeout()`]: http.md#serversettimeoutmsecs-callback
5651cb0ef41Sopenharmony_ci[`server.timeout`]: http.md#servertimeout
5661cb0ef41Sopenharmony_ci[`tls.connect()`]: tls.md#tlsconnectoptions-callback
5671cb0ef41Sopenharmony_ci[`tls.createSecureContext()`]: tls.md#tlscreatesecurecontextoptions
5681cb0ef41Sopenharmony_ci[`tls.createServer()`]: tls.md#tlscreateserveroptions-secureconnectionlistener
5691cb0ef41Sopenharmony_ci[sni wiki]: https://en.wikipedia.org/wiki/Server_Name_Indication
570