Skip to content

Commit d4a1e0d

Browse files
authored
Merge pull request #1183 from arty-name/1165-json-content-type
detect JSON by provided content-type instead of content check
2 parents 01c1207 + 18612a4 commit d4a1e0d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/http.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export default function http(url, request = {}) {
6767
// exported for testing
6868
export const shouldDownloadAsText = (contentType = '') => /json|xml|yaml|text/.test(contentType)
6969

70-
function parseBody(body) {
71-
if (/^\s*\{/.test(body)) {
70+
function parseBody(body, contentType) {
71+
if (contentType === 'application/json') {
7272
return JSON.parse(body)
7373
}
7474
return jsYaml.safeLoad(body)
@@ -84,7 +84,8 @@ export function serializeRes(oriRes, url, {loadSpec = false} = {}) {
8484
headers: serializeHeaders(oriRes.headers)
8585
}
8686

87-
const useText = loadSpec || shouldDownloadAsText(res.headers['content-type'])
87+
const contentType = res.headers['content-type']
88+
const useText = loadSpec || shouldDownloadAsText(contentType)
8889

8990
// Note: Response.blob not implemented in node-fetch 1. Use buffer instead.
9091
const getBody = useText ? oriRes.text : (oriRes.blob || oriRes.buffer)
@@ -95,7 +96,7 @@ export function serializeRes(oriRes, url, {loadSpec = false} = {}) {
9596

9697
if (useText) {
9798
try {
98-
const obj = parseBody(body)
99+
const obj = parseBody(body, contentType)
99100
res.body = obj
100101
res.obj = obj
101102
}

0 commit comments

Comments
 (0)