Fernando
Fernando

Reputation: 2189

How to receive a GZIP response with RESTClient

I am using a service that only works with GZIP compression. I added the accept header and the service works, but RESTClient can not parse the content correctly.

My code:

def client = new RESTClient('https://rest-service-gziped.com')
def postBody = [ somekey: "somevalue" ]
def response = client.post(
    path: "/some-endpoint",
    body: postBody,
    requestContentType : ContentType.JSON,
    headers: ["Accept-Encoding": "gzip"]
)

The error message is

Mar 17, 2017 5:48:03 PM groovyx.net.http.RESTClient handleResponse
WARNING: Error parsing 'application/json; charset=utf-8' response
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

Upvotes: 1

Views: 3639

Answers (2)

Bastian
Bastian

Reputation: 61

If you are using microprofile RestClientBuilder:

RestClientBuilder.newBuilder()
                .baseUri(java.net.URI.create(baseUri))
                .register(AcceptEncodingGZIPFilter.class)
                .register(GZIPDecodingInterceptor.class)
                .register(GZIPEncodingInterceptor.class)
                .build(SomeInterface.class);

To compress the content of a post request, the parameter in the method of the interface "SomeInterface" must be annotated with @GZIP:

@GZIP
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/test/data")
public ReturnObject PostRequest(@GZIP SomeData data)

The @GZIP annotation on method level (or interface level) is used to accept a compressed response.

Upvotes: 0

Fernando
Fernando

Reputation: 2189

Just add the following after RESTClient instantiation

client.setContentEncoding(ContentEncoding.Type.GZIP, ContentEncoding.Type.DEFLATE)

Upvotes: 1

Related Questions