1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.20.1">
7  <title>HTTPS | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/https.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  
14</head>
15<body class="alt apidoc" id="api-section-https">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https active">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="https" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115            
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#https">HTTPS</a></span>
124<ul>
125<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li>
126<li><a href="#class-httpsagent">Class: <code>https.Agent</code></a>
127<ul>
128<li><a href="#new-agentoptions"><code>new Agent([options])</code></a>
129<ul>
130<li><a href="#event-keylog">Event: <code>'keylog'</code></a></li>
131</ul>
132</li>
133</ul>
134</li>
135<li><a href="#class-httpsserver">Class: <code>https.Server</code></a>
136<ul>
137<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
138<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li>
139<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li>
140<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li>
141<li><a href="#serverlisten"><code>server.listen()</code></a></li>
142<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li>
143<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li>
144<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
145<li><a href="#servertimeout"><code>server.timeout</code></a></li>
146<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li>
147</ul>
148</li>
149<li><a href="#httpscreateserveroptions-requestlistener"><code>https.createServer([options][, requestListener])</code></a></li>
150<li><a href="#httpsgetoptions-callback"><code>https.get(options[, callback])</code></a></li>
151<li><a href="#httpsgeturl-options-callback"><code>https.get(url[, options][, callback])</code></a></li>
152<li><a href="#httpsglobalagent"><code>https.globalAgent</code></a></li>
153<li><a href="#httpsrequestoptions-callback"><code>https.request(options[, callback])</code></a></li>
154<li><a href="#httpsrequesturl-options-callback"><code>https.request(url[, options][, callback])</code></a></li>
155</ul>
156</li>
157</ul></div></div>
158    </li>
159  
160            
161    <li class="picker-header">
162      <a href="#">
163        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
164        Index
165      </a>
166
167      <div class="picker"><ul>
168<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
169<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
170
171      <li>
172        <a href="index.html">Index</a>
173      </li>
174    </ul>
175  
176<hr class="line">
177<ul>
178<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
179<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
180<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
181<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
182<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
183<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
184<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
185<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
186<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
187<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
188<li><a href="console.html" class="nav-console">Console</a></li>
189<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
190<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
191<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
192<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
193<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
194<li><a href="dns.html" class="nav-dns">DNS</a></li>
195<li><a href="domain.html" class="nav-domain">Domain</a></li>
196<li><a href="errors.html" class="nav-errors">Errors</a></li>
197<li><a href="events.html" class="nav-events">Events</a></li>
198<li><a href="fs.html" class="nav-fs">File system</a></li>
199<li><a href="globals.html" class="nav-globals">Globals</a></li>
200<li><a href="http.html" class="nav-http">HTTP</a></li>
201<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
202<li><a href="https.html" class="nav-https active">HTTPS</a></li>
203<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
204<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
205<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
206<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
207<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
208<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
209<li><a href="net.html" class="nav-net">Net</a></li>
210<li><a href="os.html" class="nav-os">OS</a></li>
211<li><a href="path.html" class="nav-path">Path</a></li>
212<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
213<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
214<li><a href="process.html" class="nav-process">Process</a></li>
215<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
216<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
217<li><a href="readline.html" class="nav-readline">Readline</a></li>
218<li><a href="repl.html" class="nav-repl">REPL</a></li>
219<li><a href="report.html" class="nav-report">Report</a></li>
220<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
221<li><a href="stream.html" class="nav-stream">Stream</a></li>
222<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
223<li><a href="test.html" class="nav-test">Test runner</a></li>
224<li><a href="timers.html" class="nav-timers">Timers</a></li>
225<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
226<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
227<li><a href="tty.html" class="nav-tty">TTY</a></li>
228<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
229<li><a href="url.html" class="nav-url">URL</a></li>
230<li><a href="util.html" class="nav-util">Utilities</a></li>
231<li><a href="v8.html" class="nav-v8">V8</a></li>
232<li><a href="vm.html" class="nav-vm">VM</a></li>
233<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
234<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
235<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
236<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
237<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
238</ul>
239<hr class="line">
240<ul>
241<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
242</ul></div>
243    </li>
244  
245            
246    <li class="picker-header">
247      <a href="#">
248        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
249        Other versions
250      </a>
251      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/https.html">21.x</a></li>
252<li><a href="https://nodejs.org/docs/latest-v20.x/api/https.html">20.x <b>LTS</b></a></li>
253<li><a href="https://nodejs.org/docs/latest-v19.x/api/https.html">19.x</a></li>
254<li><a href="https://nodejs.org/docs/latest-v18.x/api/https.html">18.x <b>LTS</b></a></li>
255<li><a href="https://nodejs.org/docs/latest-v17.x/api/https.html">17.x</a></li>
256<li><a href="https://nodejs.org/docs/latest-v16.x/api/https.html">16.x</a></li>
257<li><a href="https://nodejs.org/docs/latest-v15.x/api/https.html">15.x</a></li>
258<li><a href="https://nodejs.org/docs/latest-v14.x/api/https.html">14.x</a></li>
259<li><a href="https://nodejs.org/docs/latest-v13.x/api/https.html">13.x</a></li>
260<li><a href="https://nodejs.org/docs/latest-v12.x/api/https.html">12.x</a></li>
261<li><a href="https://nodejs.org/docs/latest-v11.x/api/https.html">11.x</a></li>
262<li><a href="https://nodejs.org/docs/latest-v10.x/api/https.html">10.x</a></li>
263<li><a href="https://nodejs.org/docs/latest-v9.x/api/https.html">9.x</a></li>
264<li><a href="https://nodejs.org/docs/latest-v8.x/api/https.html">8.x</a></li>
265<li><a href="https://nodejs.org/docs/latest-v7.x/api/https.html">7.x</a></li>
266<li><a href="https://nodejs.org/docs/latest-v6.x/api/https.html">6.x</a></li>
267<li><a href="https://nodejs.org/docs/latest-v5.x/api/https.html">5.x</a></li>
268<li><a href="https://nodejs.org/docs/latest-v4.x/api/https.html">4.x</a></li>
269<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/https.html">0.12.x</a></li>
270<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/https.html">0.10.x</a></li></ol></div>
271    </li>
272  
273            <li class="picker-header">
274              <a href="#">
275                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
276                Options
277              </a>
278        
279              <div class="picker">
280                <ul>
281                  <li>
282                    <a href="all.html">View on single page</a>
283                  </li>
284                  <li>
285                    <a href="https.json">View as JSON</a>
286                  </li>
287                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/https.md">Edit on GitHub</a></li>    
288                </ul>
289              </div>
290            </li>
291          </ul>
292        </div>
293        <hr>
294      </header>
295
296      <details id="toc" open><summary>Table of contents</summary><ul>
297<li><span class="stability_2"><a href="#https">HTTPS</a></span>
298<ul>
299<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li>
300<li><a href="#class-httpsagent">Class: <code>https.Agent</code></a>
301<ul>
302<li><a href="#new-agentoptions"><code>new Agent([options])</code></a>
303<ul>
304<li><a href="#event-keylog">Event: <code>'keylog'</code></a></li>
305</ul>
306</li>
307</ul>
308</li>
309<li><a href="#class-httpsserver">Class: <code>https.Server</code></a>
310<ul>
311<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
312<li><a href="#servercloseallconnections"><code>server.closeAllConnections()</code></a></li>
313<li><a href="#servercloseidleconnections"><code>server.closeIdleConnections()</code></a></li>
314<li><a href="#serverheaderstimeout"><code>server.headersTimeout</code></a></li>
315<li><a href="#serverlisten"><code>server.listen()</code></a></li>
316<li><a href="#servermaxheaderscount"><code>server.maxHeadersCount</code></a></li>
317<li><a href="#serverrequesttimeout"><code>server.requestTimeout</code></a></li>
318<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
319<li><a href="#servertimeout"><code>server.timeout</code></a></li>
320<li><a href="#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a></li>
321</ul>
322</li>
323<li><a href="#httpscreateserveroptions-requestlistener"><code>https.createServer([options][, requestListener])</code></a></li>
324<li><a href="#httpsgetoptions-callback"><code>https.get(options[, callback])</code></a></li>
325<li><a href="#httpsgeturl-options-callback"><code>https.get(url[, options][, callback])</code></a></li>
326<li><a href="#httpsglobalagent"><code>https.globalAgent</code></a></li>
327<li><a href="#httpsrequestoptions-callback"><code>https.request(options[, callback])</code></a></li>
328<li><a href="#httpsrequesturl-options-callback"><code>https.request(url[, options][, callback])</code></a></li>
329</ul>
330</li>
331</ul></details>
332
333      <div id="apicontent">
334        <h2>HTTPS<span><a class="mark" href="#https" id="https">#</a></span><a aria-hidden="true" class="legacy" id="https_https"></a></h2>
335
336<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
337<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/https.js">lib/https.js</a></p>
338<p>HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a
339separate module.</p>
340<section><h3>Determining if crypto support is unavailable<span><a class="mark" href="#determining-if-crypto-support-is-unavailable" id="determining-if-crypto-support-is-unavailable">#</a></span><a aria-hidden="true" class="legacy" id="https_determining_if_crypto_support_is_unavailable"></a></h3>
341<p>It is possible for Node.js to be built without including support for the
342<code>node:crypto</code> module. In such cases, attempting to <code>import</code> from <code>https</code> or
343calling <code>require('node:https')</code> will result in an error being thrown.</p>
344<p>When using CommonJS, the error thrown can be caught using try/catch:</p>
345<!-- eslint-skip -->
346<pre><code class="language-js cjs"><span class="hljs-keyword">let</span> https;
347<span class="hljs-keyword">try</span> {
348  https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:https'</span>);
349} <span class="hljs-keyword">catch</span> (err) {
350  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'https support is disabled!'</span>);
351}</code> <button class="copy-button">copy</button></pre>
352<p>When using the lexical ESM <code>import</code> keyword, the error can only be
353caught if a handler for <code>process.on('uncaughtException')</code> is registered
354<em>before</em> any attempt to load the module is made (using, for instance,
355a preload module).</p>
356<p>When using ESM, if there is a chance that the code may be run on a build
357of Node.js where crypto support is not enabled, consider using the
358<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"><code>import()</code></a> function instead of the lexical <code>import</code> keyword:</p>
359<pre><code class="language-js mjs"><span class="hljs-keyword">let</span> https;
360<span class="hljs-keyword">try</span> {
361  https = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:https'</span>);
362} <span class="hljs-keyword">catch</span> (err) {
363  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'https support is disabled!'</span>);
364}</code> <button class="copy-button">copy</button></pre>
365</section><section><h3>Class: <code>https.Agent</code><span><a class="mark" href="#class-httpsagent" id="class-httpsagent">#</a></span><a aria-hidden="true" class="legacy" id="https_class_https_agent"></a></h3>
366<div class="api_metadata">
367<details class="changelog"><summary>History</summary>
368<table>
369<tbody><tr><th>Version</th><th>Changes</th></tr>
370<tr><td>v5.3.0</td>
371<td><p>support <code>0</code> <code>maxCachedSessions</code> to disable TLS session caching.</p></td></tr>
372<tr><td>v2.5.0</td>
373<td><p>parameter <code>maxCachedSessions</code> added to <code>options</code> for TLS sessions reuse.</p></td></tr>
374<tr><td>v0.4.5</td>
375<td><p><span>Added in: v0.4.5</span></p></td></tr>
376</tbody></table>
377</details>
378</div>
379<p>An <a href="#class-httpsagent"><code>Agent</code></a> object for HTTPS similar to <a href="http.html#class-httpagent"><code>http.Agent</code></a>. See
380<a href="#httpsrequestoptions-callback"><code>https.request()</code></a> for more information.</p>
381<h4><code>new Agent([options])</code><span><a class="mark" href="#new-agentoptions" id="new-agentoptions">#</a></span><a aria-hidden="true" class="legacy" id="https_new_agent_options"></a></h4>
382<div class="api_metadata">
383<details class="changelog"><summary>History</summary>
384<table>
385<tbody><tr><th>Version</th><th>Changes</th></tr>
386<tr><td>v12.5.0</td>
387<td><p>do not automatically set servername if the target host was specified using an IP address.</p></td></tr>
388</tbody></table>
389</details>
390</div>
391<ul>
392<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Set of configurable options to set on the agent.
393Can have the same fields as for <a href="http.html#new-agentoptions"><code>http.Agent(options)</code></a>, and
394<ul>
395<li>
396<p><code>maxCachedSessions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> maximum number of TLS cached sessions.
397Use <code>0</code> to disable TLS session caching. <strong>Default:</strong> <code>100</code>.</p>
398</li>
399<li>
400<p><code>servername</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> the value of
401<a href="https://en.wikipedia.org/wiki/Server_Name_Indication">Server Name Indication extension</a> to be sent to the server. Use
402empty string <code>''</code> to disable sending the extension.
403<strong>Default:</strong> host name of the target server, unless the target server
404is specified using an IP address, in which case the default is <code>''</code> (no
405extension).</p>
406<p>See <a href="tls.html#session-resumption"><code>Session Resumption</code></a> for information about TLS session reuse.</p>
407</li>
408</ul>
409</li>
410</ul>
411<h5>Event: <code>'keylog'</code><span><a class="mark" href="#event-keylog" id="event-keylog">#</a></span><a aria-hidden="true" class="legacy" id="https_event_keylog"></a></h5>
412<div class="api_metadata">
413<span>Added in: v13.2.0, v12.16.0</span>
414</div>
415<ul>
416<li><code>line</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> Line of ASCII text, in NSS <code>SSLKEYLOGFILE</code> format.</li>
417<li><code>tlsSocket</code> <a href="tls.html#class-tlstlssocket" class="type">&#x3C;tls.TLSSocket></a> The <code>tls.TLSSocket</code> instance on which it was
418generated.</li>
419</ul>
420<p>The <code>keylog</code> event is emitted when key material is generated or received by a
421connection managed by this agent (typically before handshake has completed, but
422not necessarily). This keying material can be stored for debugging, as it
423allows captured TLS traffic to be decrypted. It may be emitted multiple times
424for each socket.</p>
425<p>A typical use case is to append received lines to a common text file, which is
426later used by software (such as Wireshark) to decrypt the traffic:</p>
427<pre><code class="language-js"><span class="hljs-comment">// ...</span>
428https.<span class="hljs-property">globalAgent</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function">(<span class="hljs-params">line, tlsSocket</span>) =></span> {
429  fs.<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, line, { <span class="hljs-attr">mode</span>: <span class="hljs-number">0o600</span> });
430});</code> <button class="copy-button">copy</button></pre>
431</section><section><h3>Class: <code>https.Server</code><span><a class="mark" href="#class-httpsserver" id="class-httpsserver">#</a></span><a aria-hidden="true" class="legacy" id="https_class_https_server"></a></h3>
432<div class="api_metadata">
433<span>Added in: v0.3.4</span>
434</div>
435<ul>
436<li>Extends: <a href="tls.html#class-tlsserver" class="type">&#x3C;tls.Server></a></li>
437</ul>
438<p>See <a href="http.html#class-httpserver"><code>http.Server</code></a> for more information.</p>
439<h4><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback" id="serverclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="https_server_close_callback"></a></h4>
440<div class="api_metadata">
441<span>Added in: v0.1.90</span>
442</div>
443<ul>
444<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
445<li>Returns: <a href="https.html#class-httpsserver" class="type">&#x3C;https.Server></a></li>
446</ul>
447<p>See <a href="http.html#serverclosecallback"><code>server.close()</code></a> in the <code>node:http</code> module.</p>
448<h4><code>server.closeAllConnections()</code><span><a class="mark" href="#servercloseallconnections" id="servercloseallconnections">#</a></span><a aria-hidden="true" class="legacy" id="https_server_closeallconnections"></a></h4>
449<div class="api_metadata">
450<span>Added in: v18.2.0</span>
451</div>
452<p>See <a href="http.html#servercloseallconnections"><code>server.closeAllConnections()</code></a> in the <code>node:http</code> module.</p>
453<h4><code>server.closeIdleConnections()</code><span><a class="mark" href="#servercloseidleconnections" id="servercloseidleconnections">#</a></span><a aria-hidden="true" class="legacy" id="https_server_closeidleconnections"></a></h4>
454<div class="api_metadata">
455<span>Added in: v18.2.0</span>
456</div>
457<p>See <a href="http.html#servercloseidleconnections"><code>server.closeIdleConnections()</code></a> in the <code>node:http</code> module.</p>
458<h4><code>server.headersTimeout</code><span><a class="mark" href="#serverheaderstimeout" id="serverheaderstimeout">#</a></span><a aria-hidden="true" class="legacy" id="https_server_headerstimeout"></a></h4>
459<div class="api_metadata">
460<span>Added in: v11.3.0</span>
461</div>
462<ul>
463<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>60000</code></li>
464</ul>
465<p>See <a href="http.html#serverheaderstimeout"><code>server.headersTimeout</code></a> in the <code>node:http</code> module.</p>
466<h4><code>server.listen()</code><span><a class="mark" href="#serverlisten" id="serverlisten">#</a></span><a aria-hidden="true" class="legacy" id="https_server_listen"></a></h4>
467<p>Starts the HTTPS server listening for encrypted connections.
468This method is identical to <a href="net.html#serverlisten"><code>server.listen()</code></a> from <a href="net.html#class-netserver"><code>net.Server</code></a>.</p>
469<h4><code>server.maxHeadersCount</code><span><a class="mark" href="#servermaxheaderscount" id="servermaxheaderscount">#</a></span><a aria-hidden="true" class="legacy" id="https_server_maxheaderscount"></a></h4>
470<ul>
471<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>2000</code></li>
472</ul>
473<p>See <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> in the <code>node:http</code> module.</p>
474<h4><code>server.requestTimeout</code><span><a class="mark" href="#serverrequesttimeout" id="serverrequesttimeout">#</a></span><a aria-hidden="true" class="legacy" id="https_server_requesttimeout"></a></h4>
475<div class="api_metadata">
476<details class="changelog"><summary>History</summary>
477<table>
478<tbody><tr><th>Version</th><th>Changes</th></tr>
479<tr><td>v18.0.0</td>
480<td><p>The default request timeout changed from no timeout to 300s (5 minutes).</p></td></tr>
481<tr><td>v14.11.0</td>
482<td><p><span>Added in: v14.11.0</span></p></td></tr>
483</tbody></table>
484</details>
485</div>
486<ul>
487<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>300000</code></li>
488</ul>
489<p>See <a href="http.html#serverrequesttimeout"><code>server.requestTimeout</code></a> in the <code>node:http</code> module.</p>
490<h4><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#serversettimeoutmsecs-callback" id="serversettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="https_server_settimeout_msecs_callback"></a></h4>
491<div class="api_metadata">
492<span>Added in: v0.11.2</span>
493</div>
494<ul>
495<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>120000</code> (2 minutes)</li>
496<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
497<li>Returns: <a href="https.html#class-httpsserver" class="type">&#x3C;https.Server></a></li>
498</ul>
499<p>See <a href="http.html#serversettimeoutmsecs-callback"><code>server.setTimeout()</code></a> in the <code>node:http</code> module.</p>
500<h4><code>server.timeout</code><span><a class="mark" href="#servertimeout" id="servertimeout">#</a></span><a aria-hidden="true" class="legacy" id="https_server_timeout"></a></h4>
501<div class="api_metadata">
502<details class="changelog"><summary>History</summary>
503<table>
504<tbody><tr><th>Version</th><th>Changes</th></tr>
505<tr><td>v13.0.0</td>
506<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
507<tr><td>v0.11.2</td>
508<td><p><span>Added in: v0.11.2</span></p></td></tr>
509</tbody></table>
510</details>
511</div>
512<ul>
513<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> 0 (no timeout)</li>
514</ul>
515<p>See <a href="http.html#servertimeout"><code>server.timeout</code></a> in the <code>node:http</code> module.</p>
516<h4><code>server.keepAliveTimeout</code><span><a class="mark" href="#serverkeepalivetimeout" id="serverkeepalivetimeout">#</a></span><a aria-hidden="true" class="legacy" id="https_server_keepalivetimeout"></a></h4>
517<div class="api_metadata">
518<span>Added in: v8.0.0</span>
519</div>
520<ul>
521<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>5000</code> (5 seconds)</li>
522</ul>
523<p>See <a href="http.html#serverkeepalivetimeout"><code>server.keepAliveTimeout</code></a> in the <code>node:http</code> module.</p>
524</section><section><h3><code>https.createServer([options][, requestListener])</code><span><a class="mark" href="#httpscreateserveroptions-requestlistener" id="httpscreateserveroptions-requestlistener">#</a></span><a aria-hidden="true" class="legacy" id="https_https_createserver_options_requestlistener"></a></h3>
525<div class="api_metadata">
526<span>Added in: v0.3.4</span>
527</div>
528<ul>
529<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Accepts <code>options</code> from <a href="tls.html#tlscreateserveroptions-secureconnectionlistener"><code>tls.createServer()</code></a>,
530<a href="tls.html#tlscreatesecurecontextoptions"><code>tls.createSecureContext()</code></a> and <a href="http.html#httpcreateserveroptions-requestlistener"><code>http.createServer()</code></a>.</li>
531<li><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A listener to be added to the <code>'request'</code> event.</li>
532<li>Returns: <a href="https.html#class-httpsserver" class="type">&#x3C;https.Server></a></li>
533</ul>
534<pre><code class="language-js"><span class="hljs-comment">// curl -k https://localhost:8000/</span>
535<span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:https'</span>);
536<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
537
538<span class="hljs-keyword">const</span> options = {
539  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-key.pem'</span>),
540  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-cert.pem'</span>),
541};
542
543https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
544  res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
545  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>);
546}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre>
547<p>Or</p>
548<pre><code class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:https'</span>);
549<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
550
551<span class="hljs-keyword">const</span> options = {
552  <span class="hljs-attr">pfx</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/test_cert.pfx'</span>),
553  <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'sample'</span>,
554};
555
556https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
557  res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
558  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>);
559}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre>
560</section><section><h3><code>https.get(options[, callback])</code><span><a class="mark" href="#httpsgetoptions-callback" id="httpsgetoptions-callback">#</a></span><a aria-hidden="true" class="legacy" id="https_https_get_options_callback"></a></h3>
561</section><section><h3><code>https.get(url[, options][, callback])</code><span><a class="mark" href="#httpsgeturl-options-callback" id="httpsgeturl-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="https_https_get_url_options_callback"></a></h3>
562<div class="api_metadata">
563<details class="changelog"><summary>History</summary>
564<table>
565<tbody><tr><th>Version</th><th>Changes</th></tr>
566<tr><td>v10.9.0</td>
567<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr>
568<tr><td>v7.5.0</td>
569<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr>
570<tr><td>v0.3.6</td>
571<td><p><span>Added in: v0.3.6</span></p></td></tr>
572</tbody></table>
573</details>
574</div>
575<ul>
576<li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
577<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Accepts the same <code>options</code> as
578<a href="#httpsrequestoptions-callback"><code>https.request()</code></a>, with the method set to GET by default.</li>
579<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
580</ul>
581<p>Like <a href="http.html#httpgetoptions-callback"><code>http.get()</code></a> but for HTTPS.</p>
582<p><code>options</code> can be an object, a string, or a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> object. If <code>options</code> is a
583string, it is automatically parsed with <a href="url.html#new-urlinput-base"><code>new URL()</code></a>. If it is a <a href="url.html#the-whatwg-url-api"><code>URL</code></a>
584object, it will be automatically converted to an ordinary <code>options</code> object.</p>
585<pre><code class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:https'</span>);
586
587https.<span class="hljs-title function_">get</span>(<span class="hljs-string">'https://encrypted.google.com/'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
588  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'statusCode:'</span>, res.<span class="hljs-property">statusCode</span>);
589  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'headers:'</span>, res.<span class="hljs-property">headers</span>);
590
591  res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">d</span>) =></span> {
592    process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(d);
593  });
594
595}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
596  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e);
597});</code> <button class="copy-button">copy</button></pre>
598</section><section><h3><code>https.globalAgent</code><span><a class="mark" href="#httpsglobalagent" id="httpsglobalagent">#</a></span><a aria-hidden="true" class="legacy" id="https_https_globalagent"></a></h3>
599<div class="api_metadata">
600<span>Added in: v0.5.9</span>
601</div>
602<p>Global instance of <a href="#class-httpsagent"><code>https.Agent</code></a> for all HTTPS client requests.</p>
603</section><section><h3><code>https.request(options[, callback])</code><span><a class="mark" href="#httpsrequestoptions-callback" id="httpsrequestoptions-callback">#</a></span><a aria-hidden="true" class="legacy" id="https_https_request_options_callback"></a></h3>
604</section><section><h3><code>https.request(url[, options][, callback])</code><span><a class="mark" href="#httpsrequesturl-options-callback" id="httpsrequesturl-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="https_https_request_url_options_callback"></a></h3>
605<div class="api_metadata">
606<details class="changelog"><summary>History</summary>
607<table>
608<tbody><tr><th>Version</th><th>Changes</th></tr>
609<tr><td>v16.7.0, v14.18.0</td>
610<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</p></td></tr>
611<tr><td>v14.1.0, v13.14.0</td>
612<td><p>The <code>highWaterMark</code> option is accepted now.</p></td></tr>
613<tr><td>v10.9.0</td>
614<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr>
615<tr><td>v9.3.0</td>
616<td><p>The <code>options</code> parameter can now include <code>clientCertEngine</code>.</p></td></tr>
617<tr><td>v7.5.0</td>
618<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr>
619<tr><td>v0.3.6</td>
620<td><p><span>Added in: v0.3.6</span></p></td></tr>
621</tbody></table>
622</details>
623</div>
624<ul>
625<li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
626<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Accepts all <code>options</code> from
627<a href="http.html#httprequestoptions-callback"><code>http.request()</code></a>, with some differences in default values:
628<ul>
629<li><code>protocol</code> <strong>Default:</strong> <code>'https:'</code></li>
630<li><code>port</code> <strong>Default:</strong> <code>443</code></li>
631<li><code>agent</code> <strong>Default:</strong> <code>https.globalAgent</code></li>
632</ul>
633</li>
634<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
635<li>Returns: <a href="http.html#class-httpclientrequest" class="type">&#x3C;http.ClientRequest></a></li>
636</ul>
637<p>Makes a request to a secure web server.</p>
638<p>The following additional <code>options</code> from <a href="tls.html#tlsconnectoptions-callback"><code>tls.connect()</code></a> are also accepted:
639<code>ca</code>, <code>cert</code>, <code>ciphers</code>, <code>clientCertEngine</code>, <code>crl</code>, <code>dhparam</code>, <code>ecdhCurve</code>,
640<code>honorCipherOrder</code>, <code>key</code>, <code>passphrase</code>, <code>pfx</code>, <code>rejectUnauthorized</code>,
641<code>secureOptions</code>, <code>secureProtocol</code>, <code>servername</code>, <code>sessionIdContext</code>,
642<code>highWaterMark</code>.</p>
643<p><code>options</code> can be an object, a string, or a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> object. If <code>options</code> is a
644string, it is automatically parsed with <a href="url.html#new-urlinput-base"><code>new URL()</code></a>. If it is a <a href="url.html#the-whatwg-url-api"><code>URL</code></a>
645object, it will be automatically converted to an ordinary <code>options</code> object.</p>
646<p><code>https.request()</code> returns an instance of the <a href="http.html#class-httpclientrequest"><code>http.ClientRequest</code></a>
647class. The <code>ClientRequest</code> instance is a writable stream. If one needs to
648upload a file with a POST request, then write to the <code>ClientRequest</code> object.</p>
649<pre><code class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:https'</span>);
650
651<span class="hljs-keyword">const</span> options = {
652  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
653  <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
654  <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
655  <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
656};
657
658<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
659  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'statusCode:'</span>, res.<span class="hljs-property">statusCode</span>);
660  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'headers:'</span>, res.<span class="hljs-property">headers</span>);
661
662  res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">d</span>) =></span> {
663    process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(d);
664  });
665});
666
667req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
668  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e);
669});
670req.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre>
671<p>Example using options from <a href="tls.html#tlsconnectoptions-callback"><code>tls.connect()</code></a>:</p>
672<pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
673  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
674  <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
675  <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
676  <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
677  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-key.pem'</span>),
678  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-cert.pem'</span>),
679};
680options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<span class="hljs-title class_">Agent</span>(options);
681
682<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
683  <span class="hljs-comment">// ...</span>
684});</code> <button class="copy-button">copy</button></pre>
685<p>Alternatively, opt out of connection pooling by not using an <a href="#class-httpsagent"><code>Agent</code></a>.</p>
686<pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
687  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
688  <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
689  <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
690  <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
691  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-key.pem'</span>),
692  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-cert.pem'</span>),
693  <span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span>,
694};
695
696<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
697  <span class="hljs-comment">// ...</span>
698});</code> <button class="copy-button">copy</button></pre>
699<p>Example using a <a href="url.html#the-whatwg-url-api"><code>URL</code></a> as <code>options</code>:</p>
700<pre><code class="language-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://abc:xyz@example.com'</span>);
701
702<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
703  <span class="hljs-comment">// ...</span>
704});</code> <button class="copy-button">copy</button></pre>
705<p>Example pinning on certificate fingerprint, or the public key (similar to
706<code>pin-sha256</code>):</p>
707<pre><code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:tls'</span>);
708<span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:https'</span>);
709<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>);
710
711<span class="hljs-keyword">function</span> <span class="hljs-title function_">sha256</span>(<span class="hljs-params">s</span>) {
712  <span class="hljs-keyword">return</span> crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>).<span class="hljs-title function_">update</span>(s).<span class="hljs-title function_">digest</span>(<span class="hljs-string">'base64'</span>);
713}
714<span class="hljs-keyword">const</span> options = {
715  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'github.com'</span>,
716  <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
717  <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
718  <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
719  <span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">host, cert</span>) {
720    <span class="hljs-comment">// Make sure the certificate is issued to the host we are connected to</span>
721    <span class="hljs-keyword">const</span> err = tls.<span class="hljs-title function_">checkServerIdentity</span>(host, cert);
722    <span class="hljs-keyword">if</span> (err) {
723      <span class="hljs-keyword">return</span> err;
724    }
725
726    <span class="hljs-comment">// Pin the public key, similar to HPKP pin-sha256 pinning</span>
727    <span class="hljs-keyword">const</span> pubkey256 = <span class="hljs-string">'pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU='</span>;
728    <span class="hljs-keyword">if</span> (<span class="hljs-title function_">sha256</span>(cert.<span class="hljs-property">pubkey</span>) !== pubkey256) {
729      <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification error: '</span> +
730        <span class="hljs-string">`The public key of '<span class="hljs-subst">${cert.subject.CN}</span>' `</span> +
731        <span class="hljs-string">'does not match our pinned fingerprint'</span>;
732      <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(msg);
733    }
734
735    <span class="hljs-comment">// Pin the exact certificate, rather than the pub key</span>
736    <span class="hljs-keyword">const</span> cert256 = <span class="hljs-string">'25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:'</span> +
737      <span class="hljs-string">'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16'</span>;
738    <span class="hljs-keyword">if</span> (cert.<span class="hljs-property">fingerprint256</span> !== cert256) {
739      <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification error: '</span> +
740        <span class="hljs-string">`The certificate of '<span class="hljs-subst">${cert.subject.CN}</span>' `</span> +
741        <span class="hljs-string">'does not match our pinned fingerprint'</span>;
742      <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(msg);
743    }
744
745    <span class="hljs-comment">// This loop is informational only.</span>
746    <span class="hljs-comment">// Print the certificate and public key fingerprints of all certs in the</span>
747    <span class="hljs-comment">// chain. Its common to pin the public key of the issuer on the public</span>
748    <span class="hljs-comment">// internet, while pinning the public key of the service in sensitive</span>
749    <span class="hljs-comment">// environments.</span>
750    <span class="hljs-keyword">do</span> {
751      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Subject Common Name:'</span>, cert.<span class="hljs-property">subject</span>.<span class="hljs-property">CN</span>);
752      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'  Certificate SHA256 fingerprint:'</span>, cert.<span class="hljs-property">fingerprint256</span>);
753
754      hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>);
755      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'  Public key ping-sha256:'</span>, <span class="hljs-title function_">sha256</span>(cert.<span class="hljs-property">pubkey</span>));
756
757      lastprint256 = cert.<span class="hljs-property">fingerprint256</span>;
758      cert = cert.<span class="hljs-property">issuerCertificate</span>;
759    } <span class="hljs-keyword">while</span> (cert.<span class="hljs-property">fingerprint256</span> !== lastprint256);
760
761  },
762};
763
764options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<span class="hljs-title class_">Agent</span>(options);
765<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
766  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'All OK. Server matched our pinned cert or public key'</span>);
767  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'statusCode:'</span>, res.<span class="hljs-property">statusCode</span>);
768  <span class="hljs-comment">// Print the HPKP values</span>
769  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'headers:'</span>, res.<span class="hljs-property">headers</span>[<span class="hljs-string">'public-key-pins'</span>]);
770
771  res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">d</span>) =></span> {});
772});
773
774req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
775  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>);
776});
777req.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre>
778<p>Outputs for example:</p>
779<pre><code class="language-text">Subject Common Name: github.com
780  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
781  Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=
782Subject Common Name: DigiCert SHA2 Extended Validation Server CA
783  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
784  Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=
785Subject Common Name: DigiCert High Assurance EV Root CA
786  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
787  Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=
788All OK. Server matched our pinned cert or public key
789statusCode: 200
790headers: 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</code> <button class="copy-button">copy</button></pre></section>
791        <!-- API END -->
792      </div>
793    </div>
794  </div>
795</body>
796</html>
797