Skip to content

Commit 0703318

Browse files
authoredMar 2, 2022
Fix encoding with json responseType (#1996)
1 parent dbbd317 commit 0703318

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
 

‎source/core/response.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const parseBody = (response: Response, responseType: ResponseType, parseJ
138138
}
139139

140140
if (responseType === 'json') {
141-
return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
141+
return rawBody.length === 0 ? '' : parseJson(rawBody.toString(encoding));
142142
}
143143

144144
if (responseType === 'buffer') {

‎test/encoding.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Buffer} from 'buffer';
2+
import test from 'ava';
3+
import withServer from './helpers/with-server.js';
4+
5+
test('encoding works with json', withServer, async (t, server, got) => {
6+
const json = {data: 'é'};
7+
8+
server.get('/', (_request, response) => {
9+
response.set('Content-Type', 'application-json');
10+
response.send(Buffer.from(JSON.stringify(json), 'latin1'));
11+
});
12+
13+
const response = await got('', {
14+
encoding: 'latin1',
15+
responseType: 'json',
16+
});
17+
18+
t.deepEqual(response.body, json);
19+
});

0 commit comments

Comments
 (0)
Please sign in to comment.