Repository navigation
feat(http): add a compression handler for request bodies - #905
Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
Conversation
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.
Guru R Bharadwaj (guru-bharadwaj20)
requested a review
from a team
as a code owner
November 20, 2026 14:05
| 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Overview
Adds
CompressionHandlerandCompressionHandlerOptiontokiota_http. I followed the compression handler spec and the Go and TypeScript implementations.Content-Encoding: gzipand updatesContent-Lengthto the compressed size.Content-Encodingis already set, or when aContent-Rangewith a bytes unit is present. A 415 for a request that already hadContent-Encodingis returned as is.CompressionHandlerOptionpassed with the request wins over the one given to the handler.com.microsoft.kiota.handler.compression.enable,http.request.body.compressedandhttp.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 throughcreate_with_custom_middleware, beforeRetryHandler.Testing Instructions
packages/http/httpxI ranpytest,yapf -dr kiota_http,isort kiota_http,pylint kiota_http --disable=W --rcfile=.pylintrcandmypy kiota_http. All pass on Python 3.13 (242 tests).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.create_with_custom_middleware([CompressionHandler()])at a localhttp.serverthat 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.