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

Unexpected ParseErrorwhen using afterResponse with json #761

Closed
fider opened this issue Mar 24, 2019 · 3 comments · Fixed by #775
Closed

Unexpected ParseErrorwhen using afterResponse with json #761

fider opened this issue Mar 24, 2019 · 3 comments · Fixed by #775
Labels
bug Something does not work as it should 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt ✭ help wanted ✭
Milestone

Comments

@fider
Copy link

fider commented Mar 24, 2019

Issuehunt badges

Unexpected parse error thrown by got in specific case described below.

How to reproduce:

  1. Use got@9.6.0
  2. Set default options json:true
  3. Get example afterResponse hook from docs (eg. basic auth to bearer token)
  4. Do request where bearer token is invalid and afterResponse should obtain new bearer token

Unexpected parse error will be thrown.

Details:

  • File as-promise.js
  • line 62 ( response.body = JSON.parse(response.body); )
  • response.body is already parsed to json object (second pase throws error)
  • afterResponse hook is async function and uses await getNewToken(). getNewToken is using another got instance to obtain token. Default option json is true for both got instances. No other differences between docs and my code.

szmarczak earned $80.00 by resolving this issue!

@szmarczak szmarczak added the bug Something does not work as it should label Mar 24, 2019
@szmarczak
Copy link
Collaborator

That's because the parsed response is passed to the original asPromise instance, and it tries to parse it again. Adding responseType: 'text' here should fix it. We need to make sure there are no things left like this.

@szmarczak szmarczak added this to the v10 milestone Mar 26, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $80.00 to this issue.


@szmarczak szmarczak changed the title afterResponse auth + json:true = unexpected parseError (got@9.6.0) Unexpected ParseErrorwhen using afterResponse with json Apr 22, 2019
@IssueHuntBot
Copy link

@sindresorhus has rewarded $72.00 to @szmarczak. See it on IssueHunt

  • 💰 Total deposit: $80.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $8.00

@issuehunt-oss issuehunt-oss bot added the 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt label May 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt ✭ help wanted ✭
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants