How to send Gzip Request using RestTemplate?

Viewed 362

I have the following code with large pojo (MyRequest) which I wanted to send in Gzip, but it's unable to hit the end-point that accept Gzip request. Am I creating Gzip request correctly? Do I need to send the pojo as a file?

MyRequest request = new MyRequest ();

HttpHeaders httpHeaders = HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_ENCODING, "gzip");
httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "gzip");

HttpEntity<byte[]> entity = new HttpEntity<>(compress(request), headers);
ResponseEntity<MyResponse> response = restTemplate.postForEntity(url, entity, MyResponse.class);



public static Byte[] compress(byte[] body) throws IOException {
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
    gzipOutputStream.write(body);
  }
  
  return baos.toByteArray();
}

Kindly share an example of Gzip encoding using RestTemplate, thanks

1 Answers
accept-encoding: gzip, deflate, br

Is for http clients to make requests to a server and expect an http response that is:

content-encoding: gzip

To what you are doing see this answer: https://serverfault.com/a/56707

Related