Skip to content

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

@baptistemarchand

Description

@baptistemarchand

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions