0

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
Fernando
  • 2,131
  • 3
  • 27
  • 46

2 Answers2

0

Just add the following after RESTClient instantiation

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

Fernando
  • 2,131
  • 3
  • 27
  • 46
0

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.

Bastian
  • 36
  • 5