···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···164164 case 'arrayBuffer':
165165 case 'blob':
166166 case 'formData':
167167- case 'json':
168167 case 'text':
169168 data = await response[parseAs]()
170169 break
170170+ case 'json': {
171171+ // Some servers return 200 with no Content-Length and empty body.
172172+ // response.json() would throw; read as text and parse if non-empty.
173173+ const text = await response.text()
174174+ data = text ? JSON.parse(text) : {}
175175+ break
176176+ }
171177 case 'stream':
172178 return opts.responseStyle === 'data'
173179 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···164164 case 'arrayBuffer':
165165 case 'blob':
166166 case 'formData':
167167- case 'json':
168167 case 'text':
169168 data = await response[parseAs]()
170169 break
170170+ case 'json': {
171171+ // Some servers return 200 with no Content-Length and empty body.
172172+ // response.json() would throw; read as text and parse if non-empty.
173173+ const text = await response.text()
174174+ data = text ? JSON.parse(text) : {}
175175+ break
176176+ }
171177 case 'stream':
172178 return opts.responseStyle === 'data'
173179 ? response.body
···11-import { describe, expect, it } from 'vitest';
11+import { describe, expect, it, vi } from 'vitest';
2233import { createClient } from '../client';
44···4848 expect(client.buildUrl(options)).toBe(url);
4949 });
5050});
5151+5252+describe('zero-length body handling', () => {
5353+ const client = createClient({ baseUrl: 'https://example.com' });
5454+5555+ it('returns empty object for zero-length JSON response', async () => {
5656+ const mockResponse = new Response(null, {
5757+ headers: {
5858+ 'Content-Length': '0',
5959+ 'Content-Type': 'application/json',
6060+ },
6161+ status: 200,
6262+ });
6363+6464+ const mockFetch = vi.fn().mockResolvedValue(mockResponse);
6565+6666+ const result = await client.request({
6767+ fetch: mockFetch,
6868+ method: 'GET',
6969+ url: '/test',
7070+ });
7171+7272+ expect(result.data).toEqual({});
7373+ });
7474+7575+ it('returns empty object for empty JSON response without Content-Length header (status 200)', async () => {
7676+ // Simulates a server returning an empty body with status 200 and no Content-Length header
7777+ // This is the scenario described in the issue where response.json() throws
7878+ const mockResponse = new Response('', {
7979+ headers: {
8080+ 'Content-Type': 'application/json',
8181+ },
8282+ status: 200,
8383+ });
8484+8585+ const mockFetch = vi.fn().mockResolvedValue(mockResponse);
8686+8787+ const result = await client.request({
8888+ fetch: mockFetch,
8989+ method: 'GET',
9090+ url: '/test',
9191+ });
9292+9393+ expect(result.data).toEqual({});
9494+ });
9595+9696+ it('returns empty object for empty response without Content-Length header and no Content-Type (defaults to JSON)', async () => {
9797+ // Tests the auto-detection behavior when no Content-Type is provided
9898+ const mockResponse = new Response('', {
9999+ status: 200,
100100+ });
101101+102102+ const mockFetch = vi.fn().mockResolvedValue(mockResponse);
103103+104104+ const result = await client.request({
105105+ fetch: mockFetch,
106106+ method: 'GET',
107107+ url: '/test',
108108+ });
109109+110110+ // When parseAs is 'auto' and no Content-Type header exists, it should handle empty body gracefully
111111+ expect(result.data).toBeDefined();
112112+ });
113113+});
+7-1
packages/custom-client/src/client.ts
···111111 case 'arrayBuffer':
112112 case 'blob':
113113 case 'formData':
114114- case 'json':
115114 case 'text':
116115 data = await response[parseAs]();
117116 break;
117117+ case 'json': {
118118+ // Some servers return 200 with no Content-Length and empty body.
119119+ // response.json() would throw; read as text and parse if non-empty.
120120+ const text = await response.text();
121121+ data = text ? JSON.parse(text) : {};
122122+ break;
123123+ }
118124 case 'stream':
119125 return {
120126 data: response.body,
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···251251 case 'arrayBuffer':
252252 case 'blob':
253253 case 'formData':
254254- case 'json':
255254 case 'text':
256255 data = await response[parseAs]();
257256 break;
257257+ case 'json': {
258258+ // Some servers return 200 with no Content-Length and empty body.
259259+ // response.json() would throw; read as text and parse if non-empty.
260260+ const text = await response.text();
261261+ data = text ? JSON.parse(text) : {};
262262+ break;
263263+ }
258264 case 'stream':
259265 return opts.responseStyle === 'data'
260266 ? response.body
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···399399 case "arrayBuffer":
400400 case "blob":
401401 case "formData":
402402- case "json":
403402 case "text":
404403 data = await response[parseAs]();
405404 break;
405405+ case "json": {
406406+ const text = await response.text();
407407+ data = text ? JSON.parse(text) : {};
408408+ break;
409409+ }
406410 case "stream": return {
407411 data: response.body,
408412 ...result
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···144144 case 'arrayBuffer':
145145 case 'blob':
146146 case 'formData':
147147- case 'json':
148147 case 'text':
149148 data = await response[parseAs]();
150149 break;
150150+ case 'json': {
151151+ // Some servers return 200 with no Content-Length and empty body.
152152+ // response.json() would throw; read as text and parse if non-empty.
153153+ const text = await response.text();
154154+ data = text ? JSON.parse(text) : {};
155155+ break;
156156+ }
151157 case 'stream':
152158 return {
153159 data: response.body,
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···182182 case 'arrayBuffer':
183183 case 'blob':
184184 case 'formData':
185185- case 'json':
186185 case 'text':
187186 data = await response[parseAs]();
188187 break;
188188+ case 'json': {
189189+ // Some servers return 200 with no Content-Length and empty body.
190190+ // response.json() would throw; read as text and parse if non-empty.
191191+ const text = await response.text();
192192+ data = text ? JSON.parse(text) : {};
193193+ break;
194194+ }
189195 case 'stream':
190196 return opts.responseStyle === 'data'
191197 ? response.body
···180180 case 'arrayBuffer':
181181 case 'blob':
182182 case 'formData':
183183- case 'json':
184183 case 'text':
185184 data = await response[parseAs]();
186185 break;
186186+ case 'json': {
187187+ // Some servers return 200 with no Content-Length and empty body.
188188+ // response.json() would throw; read as text and parse if non-empty.
189189+ const text = await response.text();
190190+ data = text ? JSON.parse(text) : {};
191191+ break;
192192+ }
187193 case 'stream':
188194 return opts.responseStyle === 'data'
189195 ? response.body
···219219 expect(result.data).toBeInstanceOf(Blob);
220220 expect((result.data as Blob).size).toBeGreaterThan(0);
221221 });
222222+223223+ it('returns empty object for empty JSON response without Content-Length header (status 200)', async () => {
224224+ // Simulates a server returning an empty body with status 200 and no Content-Length header
225225+ // This is the scenario described in the issue where response.json() throws
226226+ const mockResponse = new Response('', {
227227+ headers: {
228228+ 'Content-Type': 'application/json',
229229+ },
230230+ status: 200,
231231+ });
232232+233233+ const mockKy = vi.fn().mockResolvedValue(mockResponse);
234234+235235+ const result = await client.request({
236236+ ky: mockKy as Partial<KyInstance> as KyInstance,
237237+ method: 'GET',
238238+ url: '/test',
239239+ });
240240+241241+ expect(result.data).toEqual({});
242242+ });
243243+244244+ it('returns empty object for empty response without Content-Length header and no Content-Type (defaults to JSON)', async () => {
245245+ // Tests the auto-detection behavior when no Content-Type is provided
246246+ const mockResponse = new Response('', {
247247+ status: 200,
248248+ });
249249+250250+ const mockKy = vi.fn().mockResolvedValue(mockResponse);
251251+252252+ const result = await client.request({
253253+ ky: mockKy as Partial<KyInstance> as KyInstance,
254254+ method: 'GET',
255255+ url: '/test',
256256+ });
257257+258258+ // When parseAs is 'auto' and no Content-Type header exists, it should handle empty body gracefully
259259+ expect(result.data).toBeDefined();
260260+ });
222261});
223262224263describe('unserialized request body handling', () => {
···249249 case 'arrayBuffer':
250250 case 'blob':
251251 case 'formData':
252252- case 'json':
253252 case 'text':
254253 data = await response[parseAs]();
255254 break;
255255+ case 'json': {
256256+ // Some servers return 200 with no Content-Length and empty body.
257257+ // response.json() would throw; read as text and parse if non-empty.
258258+ const text = await response.text();
259259+ data = text ? JSON.parse(text) : {};
260260+ break;
261261+ }
256262 case 'stream':
257263 return opts.responseStyle === 'data'
258264 ? response.body
···142142 case 'arrayBuffer':
143143 case 'blob':
144144 case 'formData':
145145- case 'json':
146145 case 'text':
147146 data = await response[parseAs]();
148147 break;
148148+ case 'json': {
149149+ // Some servers return 200 with no Content-Length and empty body.
150150+ // response.json() would throw; read as text and parse if non-empty.
151151+ const text = await response.text();
152152+ data = text ? JSON.parse(text) : {};
153153+ break;
154154+ }
149155 case 'stream':
150156 return {
151157 data: response.body,