Skip to content

Commit

Permalink
implement parsing json response when json is truthy
Browse files Browse the repository at this point in the history
  • Loading branch information
benatkin committed Aug 2, 2011
1 parent 3acd82a commit 68c17f6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 4 additions & 1 deletion main.js
Expand Up @@ -289,7 +289,10 @@ Request.prototype.request = function () {
})
options.on("end", function () {
response.body = buffer
options.callback(null, response, buffer)
if (options.json && response.headers['content-type'] === 'application/json') {

This comment has been minimized.

Copy link
@rgrove

rgrove Aug 31, 2011

What if the content-type is "application/json;charset=utf-8"?

This comment has been minimized.

Copy link
@benatkin

benatkin Aug 31, 2011

Author Contributor

Hmm...my update to that code didn't get merged. We were just going to try parsing all responses to a request with a json property as JSON (search for "might want to"). If I were going to detect a JSON content type I might use this test: if it starts with application/json or contains +json, it's JSON.

response.body = JSON.parse(response.body)
}
options.callback(null, response, response.body)
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/test-body.js
Expand Up @@ -65,8 +65,7 @@ for (i in tests) {
request(test, function (err, resp, body) {
if (err) throw err;
if (test.expectBody) {
if (test.expectBody !== body) console.log(test.expectBody, body);
assert.equal(test.expectBody, body)
assert.deepEqual(test.expectBody, body)
}
counter = counter - 1;
if (counter === 0) {
Expand Down

1 comment on commit 68c17f6

@aseemk
Copy link

@aseemk aseemk commented on 68c17f6 Aug 14, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, this change totally broke our site, and it took a crazy long time to find and debug this...

I like the change, but it's really not nice to introduce breaking changes in a bugfix update! =/

Please sign in to comment.