diff --git a/src/core.ts b/src/core.ts index 9d90178ab..86dc31cb9 100644 --- a/src/core.ts +++ b/src/core.ts @@ -228,7 +228,7 @@ export abstract class APIClient { */ protected defaultHeaders(opts: FinalRequestOptions): Headers { return { - Accept: 'application/json', + Accept: opts.stream ? 'text/event-stream' : 'application/json', 'Content-Type': 'application/json', 'User-Agent': this.getUserAgent(), ...getPlatformHeaders(), diff --git a/tests/index.test.ts b/tests/index.test.ts index f39571121..f8521622f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -31,6 +31,11 @@ describe('instantiate client', () => { expect((req.headers as Headers)['x-my-default-header']).toEqual('2'); }); + test('streaming request must have Accept header set to text/event-stream', () => { + const { req } = client.buildRequest({ path: '/', method: 'get', stream: true }); + expect((req.headers as Headers)['accept']).toEqual('text/event-stream'); + }); + test('can ignore `undefined` and leave the default', () => { const { req } = client.buildRequest({ path: '/foo',