Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
http.response.getHeaders()
Browse files Browse the repository at this point in the history
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
langpavel committed Sep 18, 2012
1 parent 63ff449 commit 82a6d45
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
13 changes: 11 additions & 2 deletions doc/api/http.markdown
Expand Up @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions lib/http.js
Expand Up @@ -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 = {};
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions test/simple/test-http-header-read.js
Expand Up @@ -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)
Expand Down

0 comments on commit 82a6d45

Please sign in to comment.