Skip to content
This repository was archived by the owner on Jan 3, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ interface FetchCompliance {
fetch: typeof fetch;
}

const HTTP_STATUS_NO_CONTENT = 204;

export class Gaxios implements FetchCompliance {
protected agentCache = new Map<
string | URL,
Expand Down Expand Up @@ -254,6 +256,10 @@ export class Gaxios implements FetchCompliance {
opts: GaxiosOptionsPrepared,
res: Response,
): Promise<ReturnType<JSON['parse']>> {
if (res.status === HTTP_STATUS_NO_CONTENT) {
return '';
Comment thread
hakimio marked this conversation as resolved.
}

if (
opts.maxContentLength &&
res.headers.has('content-length') &&
Expand All @@ -270,8 +276,14 @@ export class Gaxios implements FetchCompliance {
switch (opts.responseType) {
case 'stream':
return res.body;
case 'json':
return res.json();
case 'json': {
const data = await res.text();
try {
return JSON.parse(data);
} catch {
return data;
}
}
case 'arraybuffer':
return res.arrayBuffer();
case 'blob':
Expand Down Expand Up @@ -605,7 +617,7 @@ export class Gaxios implements FetchCompliance {
* This implementation follows the spec: https://www.ietf.org/rfc/rfc2387.txt. However, recursive
* multipart/related requests are not currently supported.
*
* @param {GaxioMultipartOptions[]} multipartOptions the pieces to turn into a multipart/related body.
* @param {GaxiosMultipartOptions[]} multipartOptions the pieces to turn into a multipart/related body.
* @param {string} boundary the boundary string to be placed between each part.
*/
private async *getMultipartRequest(
Expand Down
39 changes: 39 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,45 @@ describe('🎏 data handling', () => {
scope.done();
}
});

it('should handle "204 No Content" responses when response type is "json"', async () => {
const scope = nock(url)
.matchHeader('content-type', 'application/json')
.put('/')
.reply(204, '', {'Content-Type': 'application/json'});
const res = await request({
url,
method: 'PUT',
data: {},
headers: new Headers({
'content-type': 'application/json',
accept: 'application/json',
}),
responseType: 'json',
});
scope.done();
assert.deepStrictEqual(res.data, '');
});

it('should not throw an error in case of invalid json and "json" response type', async () => {
const invalidJsonText = '{foo: 1}',
scope = nock(url)
.matchHeader('content-type', 'application/json')
.put('/')
.reply(200, invalidJsonText, {'Content-Type': 'application/json'});
const res = await request({
url,
method: 'PUT',
data: {},
headers: new Headers({
'content-type': 'application/json',
accept: 'application/json',
}),
responseType: 'json',
});
scope.done();
assert.deepStrictEqual(res.data, invalidJsonText);
});
});

describe('🍂 defaults & instances', () => {
Expand Down
Loading