diff --git a/src/core.ts b/src/core.ts index 10b9d3f..263883e 100644 --- a/src/core.ts +++ b/src/core.ts @@ -222,9 +222,15 @@ export abstract class APIClient { * } */ protected defaultHeaders(opts: FinalRequestOptions): Headers { + // Omit default JSON Content-Type for bodyless methods. DELETE still needs + // application/json when the caller sends a body (#180 / Codex follow-up). + const omitDefaultContentType = + opts.method === 'head' || + opts.method === 'get' || + (opts.method === 'delete' && (opts.body === undefined || opts.body === null)); return { Accept: 'application/json', - ...(['head', 'get'].includes(opts.method) ? {} : { 'Content-Type': 'application/json' }), + ...(omitDefaultContentType ? {} : { 'Content-Type': 'application/json' }), 'User-Agent': this.getUserAgent(), ...getPlatformHeaders(), ...this.authHeaders(opts), diff --git a/tests/index.test.ts b/tests/index.test.ts index 7639b23..1ca9292 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -148,6 +148,37 @@ describe('instantiate client', () => { expect(capturedRequest?.method).toEqual('PATCH'); }); + test('does not set Content-Type on DELETE (#180)', async () => { + const client = new Browserbase({ + baseURL: 'http://localhost:5000/', + apiKey: 'My API Key', + }); + const { req } = await client.buildRequest({ path: '/v1/contexts/id', method: 'delete' }); + expect(req.headers as Headers).not.toHaveProperty('content-type'); + }); + + test('sets Content-Type on DELETE when a JSON body is present', async () => { + const client = new Browserbase({ + baseURL: 'http://localhost:5000/', + apiKey: 'My API Key', + }); + const { req } = await client.buildRequest({ + path: '/v1/contexts/id', + method: 'delete', + body: { force: true }, + }); + expect((req.headers as Headers)['content-type']).toEqual('application/json'); + }); + + test('still sets Content-Type on POST', async () => { + const client = new Browserbase({ + baseURL: 'http://localhost:5000/', + apiKey: 'My API Key', + }); + const { req } = await client.buildRequest({ path: '/foo', method: 'post', body: { a: 1 } }); + expect((req.headers as Headers)['content-type']).toEqual('application/json'); + }); + describe('baseUrl', () => { test('trailing slash', () => { const client = new Browserbase({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' });