2

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

user1417746
  • 191
  • 2
  • 3
  • 11
  • why are you doing this? only _response_ body is supposed to be compressed, I could not find a single reference to an instance where *client* would be sending compressed content. See here https://en.wikipedia.org/wiki/HTTP_compression – 62mkv Mar 10 '21 at 14:39
  • 1
    You can send Gzip compressed request. The request object is of size > 5GBs. – user1417746 Mar 10 '21 at 16:06
  • apparently, you're right.. someone needs to update MDN )))) – 62mkv Mar 11 '21 at 07:43
  • you could take a look at this, I guess RestTemplate is not the only way you can execute HTTP requests in Java: https://stackoverflow.com/questions/24635190/how-to-send-httprequest-to-server-with-gzip-data/24635463 – 62mkv Mar 11 '21 at 07:46

1 Answers1

-1
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

Chris Savory
  • 2,597
  • 1
  • 17
  • 27
  • This answer would be made better by adding the important information to the answer itself, in addition to supplying the link. – Ken Wayne VanderLinde Mar 10 '21 at 19:37
  • If i could mark this question a duplicate, I would. – Chris Savory Mar 10 '21 at 19:37
  • It's deliberate the question can't be duplicates of a question on another site despite covering the same sort of things. Regardless, the two questions are entirely distinct. This is a programming question from the client-side, that is an IT admin question from the server-side. – Ken Wayne VanderLinde Mar 10 '21 at 21:07