Permalink
Browse files

http.response.getHeaders()

Reads out all headers that's already been queued but not sent to the
client.
Keys are proper-case as they will (or was) sent.  Usefull for logging

Example:

```js
console.log(response.getHeaders());
// { 'Content-Type': 'text/plain' }
```

This addressed #3992
  • Loading branch information...
1 parent 63ff449 commit 82a6d459e9afed095cab0d9d8943903eea89316f @langpavel langpavel committed Sep 18, 2012
Showing with 26 additions and 7 deletions.
  1. +11 −2 doc/api/http.markdown
  2. +9 −5 lib/http.js
  3. +6 −0 test/simple/test-http-header-read.js
View
@@ -409,13 +409,22 @@ in responses.
### response.getHeader(name)
Reads out a header that's already been queued but not sent to the client. Note
-that the name is case insensitive. This can only be called before headers get
-implicitly flushed.
+that the name is case insensitive.
Example:
var contentType = response.getHeader('content-type');
+### response.getHeaders()
+
+Reads out all headers that's already been queued but not sent to the client.
+Keys are proper-case as they will (or was) sent. Usefull for logging
+
+Example:
+
+ console.log(response.getHeaders());
+ // { 'Content-Type': 'text/plain' }
+
### response.removeHeader(name)
Removes a header that's queued for implicit sending.
View
@@ -681,11 +681,7 @@ OutgoingMessage.prototype.removeHeader = function(name) {
};
-OutgoingMessage.prototype._renderHeaders = function() {
- if (this._header) {
- throw new Error('Can\'t render headers after they are sent to the client.');
- }
-
+OutgoingMessage.prototype.getHeaders = function() {
if (!this._headers) return {};
var headers = {};
@@ -698,6 +694,14 @@ OutgoingMessage.prototype._renderHeaders = function() {
};
+OutgoingMessage.prototype._renderHeaders = function() {
+ if (this._header) {
+ throw new Error('Can\'t render headers after they are sent to the client.');
+ }
+
+ return this.getHeaders();
+};
+
OutgoingMessage.prototype.write = function(chunk, encoding) {
if (!this._header) {
@@ -29,8 +29,14 @@ var http = require('http');
var s = http.createServer(function(req, res) {
var contentType = 'Content-Type';
var plain = 'text/plain';
+
res.setHeader(contentType, plain);
+ var headers1 = res.getHeaders();
+ assert.deepEqual(headers1, {'Content-Type': 'text/plain'});
res.writeHead(200);
+ var headers2 = res.getHeaders();
+ assert.deepEqual(headers2, headers1);
+
res.end('hello world\n');
// This checks that after the headers have been sent, getHeader works
// and does not throw an exception (Issue 752)

0 comments on commit 82a6d45

Please sign in to comment.