Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions docs-js/features/connectivity/generic-http-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,82 @@ const response = await executeHttpRequest(
);
```

### Request Compression

:::caution

Not all servers support decompressing request payloads with every supported algorithm - or any algorithm at all.
Verify your target server supports decompression of compressed requests before enabling this middleware.

:::

:::info

Place compression middleware at the beginning of your middleware array.
This ensures the payload is compressed once and reused across retry attempts.

:::

The SAP Cloud SDK provides a [`compress()`](pathname:///api/v4/functions/sap-cloud-sdk_http-client.compress.html) middleware to compress HTTP request payloads using a selected compression algorithm.
This can reduce bandwidth usage and improve performance when sending large payloads to remote APIs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nth] Consider also mentioning the tradeoff between compression overhead and bandwith reduction. Compression might be worse for small payloads.
This also is a good motivation for the "auto" mode.


Compression requires additional computational resources to encode the payload, and the compressed payload could be slightly larger than the original if the payload is not suited for compression (e.g., too small or already compressed).
For small payloads or in a high-bandwidth environment, the overhead may outweigh potential bandwidth savings.
The default "auto" mode addresses this by only compressing payloads above a size threshold.

For details on supported algorithms and options, see the [API documentation](pathname:///api/v4/interfaces/sap-cloud-sdk_http-client.RequestCompressionMiddlewareOptions.html).

To enable automatic compression with `gzip` based on payload size:

```ts
import { compress } from '@sap-cloud-sdk/http-client';

const response = await executeHttpRequest(
{
url: 'https://example.com'
},
{
method: 'post',
data: largePayload,
middleware: [compress()]
}
);
```

The [`compress()`](pathname:///api/v4/functions/sap-cloud-sdk_http-client.compress.html) middleware supports [four modes of operation](pathname:///api/v4/interfaces/sap-cloud-sdk_http-client.RequestCompressionMiddlewareOptions.html#mode):

**Auto mode (recommended, default):** Compresses payloads only if they exceed a size threshold.

```ts
// Use default threshold of 1024 bytes
middleware: [compress()];
```

```ts
// Use custom threshold
middleware: [compress({ mode: 'auto', autoCompressMinSize: 5000 })];
```

**Always compress:** Forces compression regardless of payload size.

```ts
middleware: [compress({ mode: 'always', compressOptions: { level: 1 } })];
```

**Header-only mode:** Sets the `Content-Encoding` header without compressing.
Use this mode when the payload is already compressed with the selected algorithm.

```ts
middleware: [compress({ algorithm: 'zstd', mode: 'header-only' })];
```

**No compression:** Disables compression even if the middleware is included.
This mode is useful for conditional logic.

```ts
middleware: [compress({ mode: 'never' })];
```

## `executeHttpRequestWithOrigin()`

The `executeHttpRequestWithOrigin()` function is a variation of `executeHttpRequest()` which allows more fine-grained control over configuration precedence.
Expand Down