Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Stricter JSON parse when content-type header is application/json. #2

merged 1 commit into from

2 participants


Just a little fix to address a problem we had during the Chargify datacenter migration last weekend. Their API was not returning a complete response and therefore the JSON was invalid. Instead of being caught at the source, the unpatched code would allow the body to slip by as a string, causing potential havoc downstream when code get a string instead of an object.

Would love to get this in a release if you have a moment, @natevw.


@natevw natevw merged commit 61b60cc into from

Ah, hmm. Forgot this had switched over to mikeal's request is req.body normally converted automatically and our code is just a backup? Was going to just clean up and release a minor update but please could you look into this? (I'm in CET and really must be sleeping now, but will try move this forward in my morning.)


Hi @natevw. I really appreciate you looking at this so quickly. I replied to inline comments above. Happy to discuss more tomorrow and have a good night.


Heh, didn't realize that this lib was still v0.2 so just a minor release should suffice. Just npm published version 0.3.0.


Cheers. Again, grateful for the fast response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 3 deletions.
  1. +7 −3 chargify.js
10 chargify.js
@@ -28,9 +28,13 @@ Chargify.prototype.request = function(options, callback) {
request(options, function(err, res, body) {
if (err) return callback(err);
- try {
- var body = JSON.parse(body);
- } catch(e) {}
+ if (res.headers['content-type'].indexOf('application/json') !== -1 && typeof body !== 'object') {
+ try {
+ res.body = body = JSON.parse(body);
+ } catch(e) {
+ return callback(e);
+ }
+ }
callback(err, res, body);
Something went wrong with that request. Please try again.