How to avoid chunked encoding when the rest api is passing through APIMan?

2.1k views Asked by At

I believe I got an error on my app.

When I try to retrieve info from url, I got an error on JSON object. It occurs because some reason, the APIMan include a kind of encoding in the response. Here is my code:

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);

when I send a request through apiman, my response is showed with special characters:

public static void main(String[] args) {
    String uri = "https://apiman.url/apiman-gateway/org/api/1.0/method";
    String apiKey = "894fd5b3-36f0-32974-xxxxxxxx"; 
    get("token", uri, apiKey);
}

// response is 1ff8\r\n{"data":...1ff8\r\nSim"[9867,"CAR\r\n1c7d\r\n...}\r\n0\r\n\r\n

when I do not use apiman, then my response is different as showed below:

public static void main(String[] args) {
    String uri = "https://192.168.56.22:8080/app/method";
    String apiKey = "not needed"; 
    get("token", uri, apiKey);
}

// response is ok {"data":...Sim"[9867,"CAR...}

PS.:The part of '...' it was just me to not pass all data here.

Does anyone had the same problem? It seems that some characteres has different encoding type.

Edit.1:

If I try to access the apiman url directly, the response is showed ok:

https://apiman.url/apiman-gateway/org/api/1.0/method?apikey=894fd5b3-36f0-32974-xxxxxxxx

// response is ok {"data":...Sim"[9867,"CAR...}

In another test, I used the cUrl method:

curl -i -X GET -H "Content-Type:application/json" -H "X-API-Key:894fd5b3-36f0-32974-xxxxxxxx" 'https://apiman.url/apiman-gateway/org/api/1.0/method'

/* response is 1ff8
{"data":...
1ff8
Sim"[9867,"CAR
1c7d
...}
0

*/

Edit.2:

For some reason the problem occurs again.

Is there a way to solve this?

1

There are 1 answers

6
ulab On BEST ANSWER

One solution is to create a custom policy plugin and override doApply method to remove the Transfer-Encoding header if it equals chunked using ApiResponse object.

apiresponse.getHeaders().remove("Transfer-Encoding");

And add the new custom policy plugin to your services. This is a simple tutorial to create plugins