diff --git a/packages/fetch-http-handler/src/fetch-http-handler.spec.ts b/packages/fetch-http-handler/src/fetch-http-handler.spec.ts index 748572c2c11a..4066b90290b4 100644 --- a/packages/fetch-http-handler/src/fetch-http-handler.spec.ts +++ b/packages/fetch-http-handler/src/fetch-http-handler.spec.ts @@ -86,6 +86,56 @@ describe.skip(FetchHttpHandler.name, () => { expect(requestCall[0]).toBe("https://foo.amazonaws.com:443/test/?bar=baz"); }); + it("will omit body if method is GET", async () => { + const mockResponse = { + headers: { entries: jest.fn().mockReturnValue([]) }, + blob: jest.fn().mockResolvedValue(new Blob()), + }; + const mockFetch = jest.fn().mockResolvedValue(mockResponse); + + (global as any).fetch = mockFetch; + + const httpRequest = new HttpRequest({ + headers: {}, + hostname: "foo.amazonaws.com", + method: "GET", + path: "/", + body: "will be omitted", + }); + const fetchHttpHandler = new FetchHttpHandler(); + + await fetchHttpHandler.handle(httpRequest, {}); + + expect(mockFetch.mock.calls.length).toBe(1); + const requestCall = mockRequest.mock.calls[0]; + expect(requestCall[1].body).toBeUndefined(); + }); + + it("will omit body if method is HEAD", async () => { + const mockResponse = { + headers: { entries: jest.fn().mockReturnValue([]) }, + blob: jest.fn().mockResolvedValue(new Blob()), + }; + const mockFetch = jest.fn().mockResolvedValue(mockResponse); + + (global as any).fetch = mockFetch; + + const httpRequest = new HttpRequest({ + headers: {}, + hostname: "foo.amazonaws.com", + method: "HEAD", + path: "/", + body: "will be omitted", + }); + const fetchHttpHandler = new FetchHttpHandler(); + + await fetchHttpHandler.handle(httpRequest, {}); + + expect(mockFetch.mock.calls.length).toBe(1); + const requestCall = mockRequest.mock.calls[0]; + expect(requestCall[1].body).toBeUndefined(); + }); + it("will not make request if already aborted", async () => { const mockResponse = { headers: { diff --git a/packages/fetch-http-handler/src/fetch-http-handler.ts b/packages/fetch-http-handler/src/fetch-http-handler.ts index 45b5bcb1c8af..6c4e28ce0bdd 100644 --- a/packages/fetch-http-handler/src/fetch-http-handler.ts +++ b/packages/fetch-http-handler/src/fetch-http-handler.ts @@ -44,12 +44,15 @@ export class FetchHttpHandler implements HttpHandler { } } - const port = request.port; + const { port, method } = request; const url = `${request.protocol}//${request.hostname}${port ? `:${port}` : ""}${path}`; + // Request constructor doesn't allow GET/HEAD request with body + // ref: https://github.com/whatwg/fetch/issues/551 + const body = method === "GET" || method === "HEAD" ? undefined : request.body; const requestOptions: RequestInit = { - body: request.body, + body, headers: new Headers(request.headers), - method: request.method, + method: method, }; // some browsers support abort signal