4

I'm trying to compress a Post's payload with (pako.js) in an angular application and get the answer in a Java back-end application via rest communication. In the back-end, I write an interceptor, and try to decompress the request via GZIPInputStream. But,I always have a "Not in GZIP format" message.

The problem is probably in the encoding of the inputstream, but I can't figure out how to solve my issue. I test many solutions, but none of them work.

If I look at the byte[] of the input stream,this is the first indexes

[31, -62, -117, 8, 0, 0, 0, 0...

What am I doing wrong?

Angular's part

var stingtogzip = encodeURIComponent(JSON.stringify(criteria));
var gzipstring= pako.gzip(stingtogzip , { to : 'string'});

options.headers = new Headers();
 options.headers.append("Content-Encoding","gzip");
options.body = gzipstring;
options.method = 'POST';
return this.http.request(req, options)

The interceptor code:

@Provider
public class GZIPReaderInterceptor implements ReaderInterceptor {
     public Object aroundReadFrom(ReaderInterceptorContext ctx)
         throws IOException {
            String encoding = ctx.getHeaders().getFirst("Content-Encoding");
            if (!"gzip".equalsIgnoreCase(encoding)) {
                return ctx.proceed();
            }

            InputStream   gzipInputStream = new GZIPInputStream(ctx.getInputStream());
            ctx.setInputStream(gzipInputStream);
                return ctx.proceed();

 }
}
gastif
  • 81
  • 5

2 Answers2

4

finally find the solution :

-No need for encodeURIComponent

-Uses Blob to transport the data to the server

var stingtogzip = JSON.stringify(criteria);
var gzipstring= pako.gzip(stingtogzip);
var blob = new Blob([gzipString]);
options.headers = new Headers();
options.headers.append("Content-Encoding","gzip");
options.body = blob;
options.method = 'POST';
return this.http.request(req, options)
gastif
  • 81
  • 5
1

In my case was necessary the Content-Type header with charset=x-user-defined-binary:

const gzip = pako.gzip(jsonString);
const blob = new Blob([gzip]);

const headers = new Headers({
   'Content-Type': 'application/json; charset=x-user-defined-binary',
   'Content-Encoding': 'gzip'
});

const reqOptions = new RequestOptions({ headers: headers });

return this.http.put('URL', blob, reqOptions)
   .map(this.extractJSON)
   .catch((err) => this.httpErrorHandler.handleError(err));
dboldureanu
  • 551
  • 5
  • 13