Skip to content

Commit 388d125

Browse files
wenningplusaddaleax
authored andcommitted
http: expose host and protocol on ClientRequest
Allow host and protocol to be inspected. PR-URL: #33803 Fixes: #2461 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent be6aee9 commit 388d125

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/api/http.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,27 @@ added: v0.4.0
688688

689689
* {string} The request path.
690690

691+
### `request.method`
692+
<!-- YAML
693+
added: v0.1.97
694+
-->
695+
696+
* {string} The request method.
697+
698+
### `request.host`
699+
<!-- YAML
700+
added: REPLACEME
701+
-->
702+
703+
* {string} The request host.
704+
705+
### `request.protocol`
706+
<!-- YAML
707+
added: REPLACEME
708+
-->
709+
710+
* {string} The request protocol.
711+
691712
### `request.removeHeader(name)`
692713
<!-- YAML
693714
added: v1.6.0

lib/_http_client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ function ClientRequest(input, options, cb) {
213213
this.parser = null;
214214
this.maxHeadersCount = null;
215215
this.reusedSocket = false;
216+
this.host = host;
217+
this.protocol = protocol;
216218

217219
let called = false;
218220

test/parallel/test-http-outgoing-properties.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,26 @@ const OutgoingMessage = http.OutgoingMessage;
5151
msg.write('asd');
5252
assert.strictEqual(msg.writableLength, 7);
5353
}
54+
55+
{
56+
const server = http.createServer((req, res) => {
57+
res.end();
58+
server.close();
59+
});
60+
61+
server.listen(0);
62+
63+
server.on('listening', common.mustCall(() => {
64+
const req = http.request({
65+
port: server.address().port,
66+
method: 'GET',
67+
path: '/'
68+
});
69+
70+
assert.strictEqual(req.path, '/');
71+
assert.strictEqual(req.method, 'GET');
72+
assert.strictEqual(req.host, 'localhost');
73+
assert.strictEqual(req.protocol, 'http:');
74+
req.end();
75+
}));
76+
}

0 commit comments

Comments
 (0)