Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using GZIP with RestTemplate

I would like to use gzip for my request and response from the server to save the bandwidth. My server supports 100% gzip format. And what I have done so far:

HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", myAuthorization());
headers.add(HttpHeaders.ACCEPT_ENCODING, "GZIP");
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<MyParameters> request = new HttpEntity<>(parameters, headers);

ResponseEntity<? extends MyResponseRaw> response = restTemplate.exchange(url, HttpMethod.POST,
                    request, myResponse.getClass());

I put a flag to the header of the request with "Accept-Encoding" to "GZIP". But when I check in the response object from the server, it still send a normal object, not the zip object and in its header there was no "Content-Encoding" : "GZIP" also.

I do not know if I make any mistakes here, or do I need to compress also my request to gzip format? Can anyone please correct me?

like image 525
Bali Avatar asked Sep 12 '26 11:09

Bali


1 Answers

have enabled gzip compression ?

If not, put this in your properties :

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css

Adding headers only modified the headers of the request, it will not automatically do the compression.

The browser is expected to send accept gzip headers. So the server know that it could compress the answer to the client. But I think you don't need to add headers yourself. Browser should do it automatically, and spring will do it automatically. Just enable compression on spring side.

like image 169
Oreste Viron Avatar answered Sep 14 '26 18:09

Oreste Viron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!