How to consume a service with chunked-encoding transfer in java using Jersey Framework

1.7k views Asked by At

I'd like to consume a restful service of a third party software tool For some reason, I'm getting the response without the Content-Length. So, the system send a response as chunked encoding. For that reason my code gets a broken json, as shown below:

1ff8\r\n{"data":...1ff8\r\nSim"[9867,"CAR\r\n1c7d\r\n...}\r\n0\r\n\r\n
// PS.:The part of '...' it was just me to not pass all data here.

So, these characters: 1ff8\r\n ; \r\n1c7d\r\n ; \r\n0\r\n\r\n ; ... is a part of chunked encoding transfer and my app gets an error: JSONException.

I'm using Jersey and I don't have any restrictions to use another framework. Here is my code below:

Client client = Client.create();
WebResource webResource = client.resource(uri);
ClientResponse response = webResource.header("X-API-Key", xApiKey)
    .header("Content-Type", "application/json; charset=UTF-8")
    .header("Accept", "application/json")
    .header("Authorization", "Bearer " + token)
    .get(ClientResponse.class);
String json = response.getEntity(String.class);
JSONObject result = new JSONObject(json);

The software belongs to a third party software tool, and even being open source, I'll spend a huge time trying understanding the whole code, or maybe creating an plugin for that.

0

There are 0 answers