How to intercept the response pipeline? #4901
-
Hello folks! In a certain service what I consume with Kiota, if I pass on the request a parameter on the JSON object that tells the server that I want the response to be compressed, the server essentially gets the JSON response, pass thru Gzip, and send back the response a plain string encoded in base64. When getting the response, I need to read that string, decode the base64, decompress with Gzip, and then I have the JSON string which would be usable by the JSON deserializer. My question is - is there a way to configure Kiota so I can intercept the response before it is handed over to the serializer so I have a chance to Gzip decompress it? I know it is not the conventional way gzip-deflate would work in a traditional HTTP request, but I wonder if is there something I could do to leverage that weird compression. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks for trying out Kiota @galvesribeiro I'm not sure which language you are using with Kiota, however, to achieve this in C#, you can write a custom Delegating Handler that is added to the request pipeline(You can check out the compressionHandler that already does some of the stuff you wish at https://github.com/microsoft/kiota-http-dotnet/blob/main/src/Middleware/CompressionHandler.cs) Once you write the custom middleware, you can then leverage the client factory to add the handler to a client so that this is handled in the response cycles by creating a var handlers = KiotaClientFactory.CreateDefaultHandlers();
var myCustomCompressionHandler = new CompressionHandler();
handlers.Add(myCustomCompressionHandler);// add your custom handler to the list...
var httpClient = KiotaClientFactory.Create(handlers); You can then initialize the request adapter with the customised httpClient as below. var requestAdapter = new HttpClientRequestAdapter(authenticationProvider,httpClient:httpClient) |
Beta Was this translation helpful? Give feedback.
Thanks for trying out Kiota @galvesribeiro
I'm not sure which language you are using with Kiota, however, to achieve this in C#, you can write a custom Delegating Handler that is added to the request pipeline(You can check out the compressionHandler that already does some of the stuff you wish at https://github.com/microsoft/kiota-http-dotnet/blob/main/src/Middleware/CompressionHandler.cs)
Once you write the custom middleware, you can then leverage the client factory to add the handler to a client so that this is handled in the response cycles by creating a
HttpClient
as below.