diff --git a/.changeset/ripe-papers-open.md b/.changeset/ripe-papers-open.md new file mode 100644 index 0000000000..e0ea947215 --- /dev/null +++ b/.changeset/ripe-papers-open.md @@ -0,0 +1,5 @@ +--- +'@sap-cloud-sdk/openapi': minor +--- + +[New Functionality] For `Blob` bodies in HTTP-requests, use the `Blob`'s `Content-Type` as the request's `Content-Type` by default. diff --git a/packages/openapi/src/openapi-request-builder.spec.ts b/packages/openapi/src/openapi-request-builder.spec.ts index bd28749efd..2f0504bd61 100644 --- a/packages/openapi/src/openapi-request-builder.spec.ts +++ b/packages/openapi/src/openapi-request-builder.spec.ts @@ -146,6 +146,57 @@ describe('openapi-request-builder', () => { ); }); + it('uses Blob content-type as content-type header when body is a Blob with a type', async () => { + const blob = new Blob([''], { type: 'application/octet-stream' }); + const requestBuilder = new OpenApiRequestBuilder('post', '/test', { + body: blob + }); + await requestBuilder.executeRaw(destination); + expect(httpClient.executeHttpRequest).toHaveBeenCalledWith( + sanitizeDestination(destination), + expect.objectContaining({ + headers: { + requestConfig: { 'content-type': 'application/octet-stream' } + }, + data: blob + }), + expect.anything() + ); + }); + + it('does not override explicit content-type header when body is a Blob', async () => { + const blob = new Blob([''], { type: 'application/octet-stream' }); + const requestBuilder = new OpenApiRequestBuilder('post', '/test', { + body: blob, + headerParameters: { 'content-type': 'application/xml' } + }); + await requestBuilder.executeRaw(destination); + expect(httpClient.executeHttpRequest).toHaveBeenCalledWith( + sanitizeDestination(destination), + expect.objectContaining({ + headers: { requestConfig: { 'content-type': 'application/xml' } }, + data: blob + }), + expect.anything() + ); + }); + + it('does not set content-type header when Blob has no type', async () => { + const blob = new Blob(['']); + const requestBuilder = new OpenApiRequestBuilder('post', '/test', { + body: blob + }); + await requestBuilder.executeRaw(destination); + expect(httpClient.executeHttpRequest).toHaveBeenCalledWith( + sanitizeDestination(destination), + expect.objectContaining({ + headers: { requestConfig: {} }, + data: blob + }), + expect.anything() + ); + }); + it('executes a request with multipart body using executeRaw', async () => { const requestBuilder = new OpenApiRequestBuilder('post', '/test', { body: { diff --git a/packages/openapi/src/openapi-request-builder.ts b/packages/openapi/src/openapi-request-builder.ts index 5b2340738f..240e508698 100644 --- a/packages/openapi/src/openapi-request-builder.ts +++ b/packages/openapi/src/openapi-request-builder.ts @@ -376,7 +376,16 @@ export class OpenApiRequestBuilder { } private getHeaders(): OriginOptions { - const options = { requestConfig: this.parameters?.headerParameters || {} }; + const headerParameters = { ...(this.parameters?.headerParameters || {}) }; + const body = this.parameters?.body; + if ( + body instanceof Blob && + body.type && + !pickValueIgnoreCase(headerParameters, 'content-type') + ) { + headerParameters['content-type'] = body.type; + } + const options = { requestConfig: headerParameters }; if (Object.keys(this.customHeaders).length) { return { custom: this.customHeaders, ...options }; }