Skip to content

Commit 45e1b61

Browse files
authored
Merge pull request ferdikoomen#683 from dbo/ferdikoomenGH-681
ferdikoomenGH-681: Handle empty responses (status 204)
2 parents e98df33 + 44462ba commit 45e1b61

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
async function getResponseBody(response: Response): Promise<any> {
2-
try {
3-
const contentType = response.headers.get('Content-Type');
4-
if (contentType) {
5-
const isJSON = contentType.toLowerCase().startsWith('application/json');
6-
if (isJSON) {
7-
return await response.json();
8-
} else {
9-
return await response.text();
2+
if (response.status !== 204) {
3+
try {
4+
const contentType = response.headers.get('Content-Type');
5+
if (contentType) {
6+
const isJSON = contentType.toLowerCase().startsWith('application/json');
7+
if (isJSON) {
8+
return await response.json();
9+
} else {
10+
return await response.text();
11+
}
1012
}
13+
} catch (error) {
14+
console.error(error);
1115
}
12-
} catch (error) {
13-
console.error(error);
1416
}
1517
return null;
1618
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
async function getResponseBody(response: Response): Promise<any> {
2-
try {
3-
const contentType = response.headers.get('Content-Type');
4-
if (contentType) {
5-
const isJSON = contentType.toLowerCase().startsWith('application/json');
6-
if (isJSON) {
7-
return await response.json();
8-
} else {
9-
return await response.text();
2+
if (response.status !== 204) {
3+
try {
4+
const contentType = response.headers.get('Content-Type');
5+
if (contentType) {
6+
const isJSON = contentType.toLowerCase().startsWith('application/json');
7+
if (isJSON) {
8+
return await response.json();
9+
} else {
10+
return await response.text();
11+
}
1012
}
13+
} catch (error) {
14+
console.error(error);
1115
}
12-
} catch (error) {
13-
console.error(error);
1416
}
1517
return null;
1618
}

test/__snapshots__/index.spec.js.snap

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,20 @@ function getResponseHeader(response: Response, responseHeader?: string): string
233233
}
234234

235235
async function getResponseBody(response: Response): Promise<any> {
236-
try {
237-
const contentType = response.headers.get('Content-Type');
238-
if (contentType) {
239-
const isJSON = contentType.toLowerCase().startsWith('application/json');
240-
if (isJSON) {
241-
return await response.json();
242-
} else {
243-
return await response.text();
236+
if (response.status !== 204) {
237+
try {
238+
const contentType = response.headers.get('Content-Type');
239+
if (contentType) {
240+
const isJSON = contentType.toLowerCase().startsWith('application/json');
241+
if (isJSON) {
242+
return await response.json();
243+
} else {
244+
return await response.text();
245+
}
244246
}
247+
} catch (error) {
248+
console.error(error);
245249
}
246-
} catch (error) {
247-
console.error(error);
248250
}
249251
return null;
250252
}
@@ -2586,18 +2588,20 @@ function getResponseHeader(response: Response, responseHeader?: string): string
25862588
}
25872589

25882590
async function getResponseBody(response: Response): Promise<any> {
2589-
try {
2590-
const contentType = response.headers.get('Content-Type');
2591-
if (contentType) {
2592-
const isJSON = contentType.toLowerCase().startsWith('application/json');
2593-
if (isJSON) {
2594-
return await response.json();
2595-
} else {
2596-
return await response.text();
2591+
if (response.status !== 204) {
2592+
try {
2593+
const contentType = response.headers.get('Content-Type');
2594+
if (contentType) {
2595+
const isJSON = contentType.toLowerCase().startsWith('application/json');
2596+
if (isJSON) {
2597+
return await response.json();
2598+
} else {
2599+
return await response.text();
2600+
}
25972601
}
2602+
} catch (error) {
2603+
console.error(error);
25982604
}
2599-
} catch (error) {
2600-
console.error(error);
26012605
}
26022606
return null;
26032607
}

0 commit comments

Comments
 (0)