Skip to content

Commit

Permalink
Fix encoding with json responseType (#1996)
Browse files Browse the repository at this point in the history
  • Loading branch information
baptistemarchand authored Mar 2, 2022
1 parent dbbd317 commit 0703318
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/core/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const parseBody = (response: Response, responseType: ResponseType, parseJ
}

if (responseType === 'json') {
return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
return rawBody.length === 0 ? '' : parseJson(rawBody.toString(encoding));
}

if (responseType === 'buffer') {
Expand Down
19 changes: 19 additions & 0 deletions test/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Buffer} from 'buffer';
import test from 'ava';
import withServer from './helpers/with-server.js';

test('encoding works with json', withServer, async (t, server, got) => {
const json = {data: 'é'};

server.get('/', (_request, response) => {
response.set('Content-Type', 'application-json');
response.send(Buffer.from(JSON.stringify(json), 'latin1'));
});

const response = await got('', {
encoding: 'latin1',
responseType: 'json',
});

t.deepEqual(response.body, json);
});

0 comments on commit 0703318

Please sign in to comment.