Skip to content

Commit ec386ec

Browse files
committed
doc: http.clientRequest: supply missing documentation for header/trailer methods
1 parent 668fb17 commit ec386ec

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

doc/api/http.markdown

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,36 @@ srv.listen(1337, '127.0.0.1', () => {
371371
Marks the request as aborting. Calling this will cause remaining data
372372
in the response to be dropped and the socket to be destroyed.
373373

374+
### request.addTrailers(headers)
375+
376+
This method adds HTTP trailing headers (a header but at the end of the
377+
message) to the response.
378+
379+
Trailers will **only** be emitted if chunked encoding is used for the
380+
response; if it is not (e.g., if the request was HTTP/1.0), they will
381+
be silently discarded.
382+
383+
Note that HTTP requires the `Trailer` header to be sent if you intend to
384+
emit trailers, with a list of the header fields in its value.
385+
386+
Example:
387+
388+
```js
389+
var request = http.request({
390+
method: 'POST',
391+
headers: {
392+
'Content-Type': 'text/plain',
393+
'Trailer': 'Content-MD5'
394+
}
395+
});
396+
request.write(fileData);
397+
request.addTrailers({'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667'});
398+
request.end();
399+
```
400+
401+
Attempting to set a header field name or value that contains invalid characters
402+
will result in a [`TypeError`][] being thrown.
403+
374404
### request.end([data][, encoding][, callback])
375405

376406
Finishes sending the request. If any parts of the body are
@@ -395,6 +425,49 @@ That's usually what you want (it saves a TCP round-trip) but not when the first
395425
data isn't sent until possibly much later. `request.flushHeaders()` lets you bypass
396426
the optimization and kickstart the request.
397427

428+
### request.getHeader(name)
429+
430+
Reads out a header that's already been queued but not sent to the client. Note
431+
that the name is case insensitive. This can only be called before headers get
432+
implicitly flushed.
433+
434+
Example:
435+
436+
```js
437+
var contentType = request.getHeader('content-type');
438+
```
439+
440+
### request.removeHeader(name)
441+
442+
Removes a header that's queued for implicit sending.
443+
444+
Example:
445+
446+
```js
447+
request.removeHeader('Content-Encoding');
448+
```
449+
450+
### request.setHeader(name, value)
451+
452+
Sets a single header value for implicit headers. If this header already exists
453+
in the to-be-sent headers, its value will be replaced. Use an array of strings
454+
here if you need to send multiple headers with the same name.
455+
456+
Example:
457+
458+
```js
459+
request.setHeader('Content-Type', 'text/html');
460+
```
461+
462+
or
463+
464+
```js
465+
request.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
466+
```
467+
468+
Attempting to set a header field name or value that contains invalid characters
469+
will result in a [`TypeError`][] being thrown.
470+
398471
### request.setNoDelay([noDelay])
399472

400473
Once a socket is assigned to this request and is connected
@@ -668,7 +741,9 @@ response; if it is not (e.g., if the request was HTTP/1.0), they will
668741
be silently discarded.
669742

670743
Note that HTTP requires the `Trailer` header to be sent if you intend to
671-
emit trailers, with a list of the header fields in its value. E.g.,
744+
emit trailers, with a list of the header fields in its value.
745+
746+
Example:
672747

673748
```js
674749
response.writeHead(200, { 'Content-Type': 'text/plain',

0 commit comments

Comments
 (0)