Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github API returns "Problems parsing JSON" since v2.50.0 #1370

Closed
dominicbartl opened this issue Jan 23, 2015 · 7 comments
Closed

Github API returns "Problems parsing JSON" since v2.50.0 #1370

dominicbartl opened this issue Jan 23, 2015 · 7 comments

Comments

@dominicbartl
Copy link

I'm making a POST request to https://api.github.com/repos/bartinger/grunt-github-release-asset/releases to create a release.

I used v2.47.0 before and everything worked fine, but when I upgraded to v2.51.0 I got 400 Problems parsing JSON even though the json was valid. I narrowed it down and found out that a change between v2.49.0 and v2.50.0 is causing this error.

I also enabled the debug mode and made a diff of the complete output, however both outputs where identical (besides dates and stuff).

Is this a bug in the package?
The code to reproduce the error is createRelease(...)

@nylen
Copy link
Member

nylen commented Jan 23, 2015

Which debug method did you use, NODE_DEBUG=request or request-debug? Try request-debug if you haven't already.

That, or a standalone example that reproduces the problem, will be most helpful.

@nylen
Copy link
Member

nylen commented Jan 23, 2015

@simov I suspect this is related to #1309 and #1310.

@dominicbartl
Copy link
Author

With request.debug = true so (NODE_DEBUG=request). I'm going to try it with request-debug as well.

@dominicbartl
Copy link
Author

Here is the complete diff between v2.49.0(upper) and v2.51.0(lower). The Validation Failed error is expected. It seemes to me that the body is stringified twice in 51. I call JSON.stringify(data) and pass the string to request.

17c17
<         date: 'Fri, 23 Jan 2015 21:29:13 GMT',
---
>         date: 'Fri, 23 Jan 2015 21:27:23 GMT',
22c22
<         'x-ratelimit-remaining': '4954',
---
>         'x-ratelimit-remaining': '4960',
37c37
<         'x-github-request-id': 'B2BC6CCA:1D6A:58C506C:54C2BD29',
---
>         'x-github-request-id': 'B2BC6CCA:1D69:3D1E7A1:54C2BCBB',
40c40
<         'x-served-by': '2811da37fbdda4367181b328b22b2499' },
---
>         'x-served-by': '474556b853193c38f1b14328ce2d1b7d' },
53,54c53,55
<         'content-length': 53 },
<      body: '{"tag_name":"0.1.1","name":"Release 0.1.1","body":""}' } }
---
>         'content-type': 'application/json',
>         'content-length': 67 },
>      body: '"{\\"tag_name\\":\\"0.1.1\\",\\"name\\":\\"Release 0.1.1\\",\\"body\\":\\"\\"}"' } }
59c60
<         date: 'Fri, 23 Jan 2015 21:29:14 GMT',
---
>         date: 'Fri, 23 Jan 2015 21:27:24 GMT',
61,62c62,63
<         'content-length': '195',
<         status: '422 Unprocessable Entity',
---
>         'content-length': '89',
>         status: '400 Bad Request',
64c65
<         'x-ratelimit-remaining': '4953',
---
>         'x-ratelimit-remaining': '4959',
75c76
<         'x-github-request-id': 'B2BC6CCA:1675:B671514:54C2BD2A',
---
>         'x-github-request-id': 'B2BC6CCA:1D68:26F3BE3:54C2BCBB',
78c79
<      statusCode: 422,
---
>      statusCode: 400,
80,83c81,83
<       { message: 'Validation Failed',
<         errors: [Object],
<         documentation_url: 'https://developer.github.com/v3/repos/releases/#create-a-release' } } }
< Fatal error: Validation Failed(422)
---
>       { message: 'Problems parsing JSON',
>         documentation_url: 'https://developer.github.com/v3' } } }
> Fatal error: Problems parsing JSON(400)

@nylen
Copy link
Member

nylen commented Jan 23, 2015

Ah, I see. Here's your fix:

## github.js
 Github.prototype._request = function (method, url, body, callback) {
    return request({
        method: method,
        url: url,
        headers: this.headers,
-       body: body ? JSON.stringify(body) : undefined,
+       body: body || undefined,
        json: true,
    }, callbackWrapper(callback));
 }

request will call JSON.stringify for you. We used to only do this if body was an object, but now, we support sending primitive JSON values like strings.

(btw, GitHub supports diff syntax highlighting nicely, I edited your comment accordingly.)

@nylen nylen closed this as completed Jan 23, 2015
@simov
Copy link
Member

simov commented Jan 23, 2015

@Bartinger you should not stringify the value of the body key if the application/x-www-form-urlencoded header value is not being set, can you try with passing the raw JSON object to the body key?

Yep that change again #1282

@nylen you replied in place of me while I was searching for the commit 👍

@dominicbartl
Copy link
Author

Already removed my stringify. I was assuming something like that. Passing the object to the body works. Thanks a lot for the quick help and the Github tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants