Skip to content

Commit

Permalink
In sails.request(), try to parse json body if simulated http client r…
Browse files Browse the repository at this point in the history
…esponse is a string. Fail silently and keep the original value if it doesnt worok
  • Loading branch information
mikermcneil committed May 13, 2014
1 parent ac3b263 commit 00154a5
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/app/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ module.exports = function request( /* address, body, cb */ ) {
// Don't include body if it is empty
if (!clientRes.body) delete clientRes.body;

// Try to interpret response body as JSON
try {
if (typeof clientRes.body === 'string') {
var jsonParsedBody = JSON.parse(clientRes.body);
clientRes.body = jsonParsedBody;
}
}
catch(e) {}

// If status code is indicative of an error, send the
// response body or status code as the first error argument.
if (clientRes.statusCode < 200 || clientRes.statusCode >= 400) {
Expand Down

0 comments on commit 00154a5

Please sign in to comment.