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

The encoding option is not working when combined with responseType: 'json' #1988

Closed
2 tasks done
baptistemarchand opened this issue Feb 24, 2022 · 3 comments · Fixed by #1996
Closed
2 tasks done

The encoding option is not working when combined with responseType: 'json' #1988

baptistemarchand opened this issue Feb 24, 2022 · 3 comments · Fixed by #1996
Labels
bug Something does not work as it should good for beginner This issue is easy to fix ✭ help wanted ✭

Comments

@baptistemarchand
Copy link
Contributor

Describe the bug

  • Node.js version: v14.18.3
  • OS & version: OSX 12.1

The encoding option is not working when combined with responseType: 'json'.

Actual behavior

I created a minimal repro repo : https://github.com/baptistemarchand/got-encoding-repro/blob/main/index.mjs

  const res1 = await got('https://latin1-api.vercel.app/', {
    encoding: 'latin1',
    responseType: 'json',
  })
  // Outputs: {data: "?"} 
  console.log('res1', res1.body)

  const res2 = await got('https://latin1-api.vercel.app/', {
    encoding: 'latin1',
  })
  // Outputs: {data: "é"} 
  console.log('res2', JSON.parse(res2.body))

What it does :
I have a dummy api that returns a JSON with the latin1 encoding : https://latin1-api.vercel.app/.
I call it twice with got :

  • once with responseType: 'json' => the encoding seems to be ignored
  • once without responseType: 'json' and instead doing a JSON.parse manually => encoding working as expected
    Here is the result :
    image

Expected behavior

I expected the result of both console.log to be the same. Like this :

res1 { data: 'é' }
res2 { data: 'é' }

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak szmarczak added bug Something does not work as it should good for beginner This issue is easy to fix ✭ help wanted ✭ labels Feb 26, 2022
@szmarczak
Copy link
Collaborator

Good catch!

return rawBody.length === 0 ? '' : parseJson(rawBody.toString());

should have an encoding argument like this one:

return rawBody.toString(encoding);

Would you be up to sending a fix along with a test for this?

@baptistemarchand
Copy link
Contributor Author

@szmarczak I cloned the repo and ran npm install and got the following TypeScript error
image

I have node v14.18.3.
Should I open a new issue for that?

@szmarczak
Copy link
Collaborator

Yes please.

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 good for beginner This issue is easy to fix ✭ help wanted ✭
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants