Skip to content

feat(http): add a compression handler for request bodies - #905

Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
microsoft:mainfrom
guru-bharadwaj20:feat/http-compression-handler
Open

Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
microsoft:mainfrom
guru-bharadwaj20:feat/http-compression-handler

Conversation

@guru-bharadwaj20

@guru-bharadwaj20 Guru R Bharadwaj (guru-bharadwaj20) commented Nov 20, 2026 •

Copy link
Copy Markdown

Overview

Adds CompressionHandler and CompressionHandlerOption to kiota_http. I followed the compression handler spec and the Go and TypeScript implementations.

  • The handler gzips the request body, sets Content-Encoding: gzip and updates Content-Length to the compressed size.
  • If the server answers 415, it sends the original uncompressed request once more.
  • It passes the request through unchanged when compression is disabled, when there is no body, when Content-Encoding is already set, or when a Content-Range with a bytes unit is present. A 415 for a request that already had Content-Encoding is returned as is.
  • A CompressionHandlerOption passed with the request wins over the one given to the handler.
  • The span records com.microsoft.kiota.handler.compression.enable, http.request.body.compressed and http.request.body.size.

The compressed request is a copy, so the original is still intact for the 415 resend. Streamed bodies are read into memory first.

Related Issue

Fixes #362

Notes

I did not add the handler to get_default_middleware. kiota-typescript also leaves it out of the default chain, and the spec lists it with the Graph handlers. Turning it on by default would change what every client sends, so I left that decision to you. For now it is added through create_with_custom_middleware, before RetryHandler.

Testing Instructions

  • In packages/http/httpx I ran pytest, yapf -dr kiota_http, isort kiota_http, pylint kiota_http --disable=W --rcfile=.pylintrc and mypy kiota_http. All pass on Python 3.13 (242 tests).
  • The new tests are in tests/middleware_tests/test_compression_handler.py. They cover compression, streamed bodies, option precedence, the skip cases, the single 415 resend, and the retry handler resending the compressed body.
  • I also pointed a client built with create_with_custom_middleware([CompressionHandler()]) at a local http.server that returns 415 for gzip bodies on one path. The first path received a gzip body. The second received a gzip body and then the plain body.

Adds CompressionHandler and CompressionHandlerOption to kiota_http,
following the middleware spec in msgraph-sdk-design:

- gzip the request body and set Content-Encoding: gzip
- on a 415 response, send the request once more uncompressed
- skip requests without a body, with an existing Content-Encoding,
  or with a bytes Content-Range
- the per-request option takes precedence over the handler option

The handler is not added to the default middleware, the same as in
kiota-typescript. It should be placed before the RetryHandler.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 14:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Synchronous gzip compression blocks the async client’s event loop during large uploads, delaying unrelated requests and timers.

1 open issue

Review effort: Balanced

if not body:
return await super().send(request, transport)

compressed_request = self._build_compressed_request(request, gzip.compress(body))

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

add support for request body compression

2 participants