Spring-cloud-gateway: Can't support compress with gzip when used ModifyResponseBody

Created on 19 Dec 2019  Â·  10Comments  Â·  Source: spring-cloud/spring-cloud-gateway

Hi:
When I used 'ModifyResponseBody' filter with my request header contained 'Accept-Encoding: gzip' , 'DecompressionException' was thrown while WebClient trying to decompress the response body。

Java8
Spring Boot Version: 2.1.4.RELEASE
Spring cloud: Greenwich.RELEASE
image
image
image
image

enhancement help wanted

Most helpful comment

Please upgrade to Hoxton.SR3

All 10 comments

Sorry, screenshots don't really tell me how to recreate the issue. Also, Greenwich.RELEASE is not the latest, please try again with Greenwich.SR4.

Sorry, screenshots don't really tell me how to recreate the issue. Also, Greenwich.RELEASE is not the latest, please try again with Greenwich.SR4.

It's not a problem of version,When I use the Greenwich.SR4, it still reports this error。Well,you can run my project test class to recreate the issue.
image

gateway-predicate.zip

So, I think the error is actually in WebClient because if I change the test to use RestTemplate there is no error.

So, I think the error is actually in WebClient because if I change the test to use RestTemplate there is no error.

But if I didn't use 'ModifyResponseBodyFilter' ,WebClient work well.Did you add HttpHeader 'Accept-Encoding gzip' when you use RestTemplate? And if I add HttpHeader 'Accept-Encoding gzip' ,the modifyResponseBody can't decode the responsebody.
image
image

Hi,
had a similar issue today(but without using any WebClients, even as simple example as (exchange, s) -> { Mono.just(s) } doesn't work with gzip, The workaround that worked for me is to use byte[].class instead of String.class as the first parameter for the modifyResponseBody method. Then you can use GZIPInputStream to decode the request body and modify it, if you want to gzip it after modification you then also need to use byte[] as a output parameter and encode it back via the GZIPOutputStream.

However I think that it can be implemented within the response modification filter within the library.
Something like:

if (exchange.getRequest().getHeaders().get(HttpHeaders.CONTENT_ENCODING).contains("gzip") && !inClass.isInstance(new byte[0])) { //..decode... }
Obviously it needs more null checks and so on, but the idea is smth like that.

Happy to implement and make a pull request if you think that it's worth implementing within the library.

PRs welcome

I have the same problem. I tryed to use library version "2.1.5.RELEASE" and "2.2.1.RELEASE". Kotlin.

gatewayFilterSpec.modifyResponseBody(String::class.java, String::class.java,
         MediaType.APPLICATION_JSON_VALUE) { exchange, s ->
     Mono.just(s)
}

When I'm receiving the answer from soapui mock-server, it has header Content-Encoding=gzip:
изображение

and the body is:
изображение

But if I remove the modifyResponseBody response is returned well.

@wellusion this has been fixed with this merge request: https://github.com/spring-cloud/spring-cloud-gateway/pull/1548

Please upgrade to Hoxton.SR3

Please upgrade to Hoxton.SR3

Sometimes you need to look back...See what i find !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chsi13 picture chsi13  Â·  6Comments

manishonline picture manishonline  Â·  3Comments

samtonyclarke picture samtonyclarke  Â·  3Comments

dennis-menge picture dennis-menge  Â·  4Comments

xfworld picture xfworld  Â·  3Comments