Skip to content

Commit 50e9f8d

Browse files
FlarnaBridgeAR
authored andcommitted
doc: improve http.setHeader and getHeader typeinfo
http.setHeader() coerces input values. http.getHeader() returns the type as passed to setHeader(). PR-URL: #19902 Fixes: #13825 Reviewed-By: Chen Gang <gangc.cxy@foxmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a37e267 commit 50e9f8d

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

doc/api/http.md

+36-7
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,24 @@ added: v1.6.0
587587
-->
588588

589589
* `name` {string}
590-
* Returns: {string}
590+
* Returns: {any}
591591

592592
Reads out a header on the request. Note that the name is case insensitive.
593+
The type of the return value depends on the arguments provided to
594+
[`request.setHeader()`][].
593595

594596
Example:
595597
```js
598+
request.setHeader('content-type', 'text/html');
599+
request.setHeader('Content-Length', Buffer.byteLength(body));
600+
request.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
596601
const contentType = request.getHeader('Content-Type');
602+
// contentType is 'text/html'
603+
const contentLength = request.getHeader('Content-Length');
604+
// contentLength is of type number
605+
const setCookie = request.getHeader('set-cookie');
606+
// setCookie is of type string[]
607+
597608
```
598609

599610
### request.removeHeader(name)
@@ -616,11 +627,14 @@ added: v1.6.0
616627
-->
617628

618629
* `name` {string}
619-
* `value` {string}
630+
* `value` {any}
620631

621632
Sets a single header value for headers object. If this header already exists in
622633
the to-be-sent headers, its value will be replaced. Use an array of strings
623-
here to send multiple headers with the same name.
634+
here to send multiple headers with the same name. Non-string values will be
635+
stored without modification. Therefore, [`request.getHeader()`][] may return
636+
non-string values. However, the non-string values will be converted to strings
637+
for network transmission.
624638

625639
Example:
626640
```js
@@ -1085,15 +1099,24 @@ added: v0.4.0
10851099
-->
10861100

10871101
* `name` {string}
1088-
* Returns: {string}
1102+
* Returns: {any}
10891103

10901104
Reads out a header that's already been queued but not sent to the client.
1091-
Note that the name is case insensitive.
1105+
Note that the name is case insensitive. The type of the return value depends
1106+
on the arguments provided to [`response.setHeader()`][].
10921107

10931108
Example:
10941109

10951110
```js
1111+
response.setHeader('Content-Type', 'text/html');
1112+
response.setHeader('Content-Length', Buffer.byteLength(body));
1113+
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
10961114
const contentType = response.getHeader('content-type');
1115+
// contentType is 'text/html'
1116+
const contentLength = response.getHeader('Content-Length');
1117+
// contentLength is of type number
1118+
const setCookie = response.getHeader('set-cookie');
1119+
// setCookie is of type string[]
10971120
```
10981121

10991122
### response.getHeaderNames()
@@ -1204,11 +1227,14 @@ added: v0.4.0
12041227
-->
12051228

12061229
* `name` {string}
1207-
* `value` {string | string[]}
1230+
* `value` {any}
12081231

12091232
Sets a single header value for implicit headers. If this header already exists
12101233
in the to-be-sent headers, its value will be replaced. Use an array of strings
1211-
here to send multiple headers with the same name.
1234+
here to send multiple headers with the same name. Non-string values will be
1235+
stored without modification. Therefore, [`response.getHeader()`][] may return
1236+
non-string values. However, the non-string values will be converted to strings
1237+
for network transmission.
12121238

12131239
Example:
12141240

@@ -2013,11 +2039,14 @@ not abort the request or do anything besides add a `'timeout'` event.
20132039
[`net.createConnection()`]: net.html#net_net_createconnection_options_connectlistener
20142040
[`removeHeader(name)`]: #http_request_removeheader_name
20152041
[`request.end()`]: #http_request_end_data_encoding_callback
2042+
[`request.getHeader()`]: #http_request_getheader_name
2043+
[`request.setHeader()`]: #http_request_setheader_name_value
20162044
[`request.setTimeout()`]: #http_request_settimeout_timeout_callback
20172045
[`request.socket`]: #http_request_socket
20182046
[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
20192047
[`request.write(data, encoding)`]: #http_request_write_chunk_encoding_callback
20202048
[`response.end()`]: #http_response_end_data_encoding_callback
2049+
[`response.getHeader()`]: #http_response_getheader_name
20212050
[`response.setHeader()`]: #http_response_setheader_name_value
20222051
[`response.socket`]: #http_response_socket
20232052
[`response.write()`]: #http_response_write_chunk_encoding_callback

0 commit comments

Comments
 (0)