8

I'm trying to send a bunch of gzip compressed data (basically a json, compressed using gzip) to my Spring Web Flux application.

I'm currently using Postman as my web client, and sending the data via request payload (Body -> raw). Here's a glimpse of my payload.

[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -51, ... (there's a lot more)

Using the Content-Type: application/json and Content-Encoding:gzip.

And this is how I'm extracting the data out of the http server request.

Mono<DataBuffer> dataBufferMono = request.flatMap(req -> req.bodyToMono(DataBuffer.class)
//...stuff to get dataBuffer out of Mono
byte[] byteArray = IOUtils.toByteArray(dataBuffer.asInputStream());

This byteArray I extracted should be the one that is sent from the client. But the problem is, it isn't. Here's a glimpse of what I received.

[91, 51, 49, 44, ... (there's a lot more)

Where am I doing it wrong? I'm stuck at work with this issue, any help will be greatly appreciated.

Note: I have also tried changing the Content-Type to octet-stream, but no luck! Also tried using ByteBuffer instead of DataBuffer, never worked.

Aryan Venkat
  • 679
  • 1
  • 10
  • 34
  • 1
    are you sure the original encoding is preserved? – Dmitry Senkovich Aug 07 '18 at 09:48
  • 1
    also, have you tried content-encoding header? – Dmitry Senkovich Aug 07 '18 at 09:56
  • 1
    I have tried Content-Encoding: gzip header. @DmitrySenkovich – Aryan Venkat Aug 07 '18 at 10:11
  • 1
    and what about encoding, have you tried `toByteArray` specifying UTF-8 explicitly? take a look here https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html#toByteArray(java.io.Reader,%20java.nio.charset.Charset) – Dmitry Senkovich Aug 07 '18 at 10:15
  • 1
    also, have you tried the generated by Postman request with curl in a terminal? it may add some funny confusing headers – Dmitry Senkovich Aug 07 '18 at 10:18
  • The IOUtils method that accepts InputStream as an argument, doesn't accept encoding. https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html#toByteArray(java.io.InputStream) – Aryan Venkat Aug 07 '18 at 10:33
  • Tried with cURL on terminal as well, no luck! No confusing headers as such. @DmitrySenkovich – Aryan Venkat Aug 07 '18 at 10:35
  • could you please share the curl request? you can change host,port,path. I will try to reproduce. or even a project on GitHub. Then I will be able to fix the problem 100% – Dmitry Senkovich Aug 07 '18 at 10:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177560/discussion-between-aryan-venkat-and-dmitry-senkovich). – Aryan Venkat Aug 07 '18 at 10:42
  • Could you try, in a classic java main method, to encode your source data using IOUtils and try to decode it, as you did in your example, and see if you see the same differences between both source and result ? – Anthony BONNIER Aug 14 '18 at 07:39
  • I could imagine, that spring is smart enough to unzip a zipped stream, based on the Content-Encoding. At least that what I'd expect a HTTP handling framework to do for me. – Frischling Mar 14 '19 at 09:03

0 Answers0