Skip to content
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
4 changes: 2 additions & 2 deletions src/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Request extends Body {
}

if (!headers.has('content-type')) {
if (isPlainObject(body)) {
// non-spec extension: support plain js object body (JSON serialization)
if (isPlainObject(body) || Array.isArray(body)) {
// non-spec extension: support plain js object/array body (JSON serialization)
body = JSON.stringify(body);
headers.set('content-type', 'application/json');
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class Response extends Body {
}

if (respBody !== null && !headers.has('content-type')) {
if (isPlainObject(respBody)) {
// non-spec extension: support plain js object body (JSON serialization)
if (isPlainObject(respBody) || Array.isArray(respBody)) {
// non-spec extension: support plain js object/array body (JSON serialization)
respBody = JSON.stringify(respBody);
headers.set('content-type', 'application/json');
} else {
Expand Down
20 changes: 20 additions & 0 deletions test/fetch/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,26 @@ describe('Request Tests', () => {
});
});

it('should serialize plain object body to JSON', () => {
const method = 'POST';
const body = { foo: 42 };
const req = new Request(BASE_URL, { method, body });
expect(req.headers.get('content-type')).to.equal('application/json');
return req.json().then((result) => {
expect(result).to.deep.equal(body);
});
});

it('should serialize array body to JSON', () => {
const method = 'POST';
const body = [1, 2, 3];
const req = new Request(BASE_URL, { method, body });
expect(req.headers.get('content-type')).to.equal('application/json');
return req.json().then((result) => {
expect(result).to.deep.equal(body);
});
});

it('wrapping requests preserves init options', () => {
const method = 'POST';
const body = { foo: 'bar', baz: { count: 313 } };
Expand Down
10 changes: 10 additions & 0 deletions test/fetch/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ describe('Response Tests', () => {
// plain js object body
res = new Response({ foo: 42 });
expect(res.headers.get('content-type')).to.equal('application/json');
// array body
res = new Response([1, 2, 3]);
expect(res.headers.get('content-type')).to.equal('application/json');
});

it('should serialize array body to JSON', () => {
const res = new Response([1, 2, 3]);
return res.json().then((result) => {
expect(result).to.deep.equal([1, 2, 3]);
});
});

it('should not override content-type header', () => {
Expand Down
Loading