0

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.

Celso Agra
  • 1,389
  • 2
  • 15
  • 37
  • Are you setting the `ClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE`? By default it's disabled. A value <= 0 should enable chunked encoding with the default size, and a value > 0 will be used as the chunk size. – sisyphus Jan 03 '17 at 14:32
  • hm... I'm not setting this property. I tried to set `config.getProperties().put(DefaultClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE, 0);` and added this config to my client: `Client client = Client.create(config);`. But this problem still occurs – Celso Agra Jan 03 '17 at 14:57
  • I saw the same issue [here](http://stackoverflow.com/a/28953216/2130322), but not work with me. I put the `PROPERTY_ENABLE_BUFFERING` as `true` – Celso Agra Jan 03 '17 at 15:15
  • 1
    have a look at http://stackoverflow.com/questions/41483716/chunkedinput-not-working-in-jersey – gladiator Jan 06 '17 at 12:52
  • @RavikantSharma thanks for your reply. This code help me to get the chuncks separated. but for some reason I got the first part as hexadecimal character (`1ff8`), then the next part is a json, the next one is another hexadecimal character (`1ff8`), then I got part of json and so on, until the `0` character (the last one). – Celso Agra Jan 09 '17 at 12:39
  • I believe this is part of backend server because I got the same answer (with hexadecimal characters) using the `curl` method from linux. – Celso Agra Jan 09 '17 at 12:42

0 Answers0