Skip to content

Commit a126cbc

Browse files
leo6104AndrewKushnir
authored andcommitted
fix(http): use serializeBody to support JSON payload in FetchBackend (#50776)
`HttpRequest.serializeBody` was used in HttpXhrBackend. `fetch` also needs to serialize request body. Close #50775 PR Close #50776
1 parent 64745a8 commit a126cbc

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

packages/common/http/src/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export class FetchBackend implements HttpBackend {
221221
}
222222

223223
return {
224-
body: req.body,
224+
body: req.serializeBody(),
225225
method: req.method,
226226
headers,
227227
credentials,

packages/common/http/test/fetch_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const TEST_POST = new HttpRequest('POST', '/test', 'some body', {
3333
responseType: 'text',
3434
});
3535

36+
const TEST_POST_WITH_JSON_BODY = new HttpRequest('POST', '/test', {'some': 'body'}, {
37+
responseType: 'text',
38+
});
39+
3640
const XSSI_PREFIX = ')]}\'\n';
3741

3842
describe('FetchBackend', async () => {
@@ -109,6 +113,11 @@ describe('FetchBackend', async () => {
109113
expect(fetchMock.request.body).toBe('some body');
110114
});
111115

116+
it('sets outgoing body correctly when request payload is json', () => {
117+
callFetchAndFlush(TEST_POST_WITH_JSON_BODY);
118+
expect(fetchMock.request.body).toBe('{"some":"body"}');
119+
});
120+
112121
it('sets outgoing headers, including default headers', () => {
113122
const post = TEST_POST.clone({
114123
setHeaders: {

packages/common/http/test/xhr_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const TEST_POST = new HttpRequest('POST', '/test', 'some body', {
2424
responseType: 'text',
2525
});
2626

27+
const TEST_POST_WITH_JSON_BODY = new HttpRequest('POST', '/test', {'some': 'body'}, {
28+
responseType: 'text',
29+
});
30+
2731
const XSSI_PREFIX = ')]}\'\n';
2832

2933
{
@@ -49,6 +53,10 @@ const XSSI_PREFIX = ')]}\'\n';
4953
backend.handle(TEST_POST).subscribe();
5054
expect(factory.mock.body).toBe('some body');
5155
});
56+
it('sets outgoing body correctly when request payload is json', () => {
57+
backend.handle(TEST_POST_WITH_JSON_BODY).subscribe();
58+
expect(factory.mock.body).toBe('{"some":"body"}');
59+
});
5260
it('sets outgoing headers, including default headers', () => {
5361
const post = TEST_POST.clone({
5462
setHeaders: {

0 commit comments

Comments
 (0)