如何在其他api通过APIMan时避免分块编码?

问题描述:

我相信我的应用程序出错了。如何在其他api通过APIMan时避免分块编码?

当我尝试从url检索信息时,JSON对象上出现错误。 这是因为某种原因发生的,APIMan在响应中包含了一种编码。这里是我的代码:

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

当我将通过apiman的请求,我的回答是表现出有特殊字符:

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 

当我不使用apiman,那么我的反应是不同的,显示如下:

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一部分“...”这只是我不在这里传递的所有数据。

有没有人有同样的问题?看起来一些字符有不同的编码类型。

Edit.1:

如果我尝试直接访问apiman URL,响应表明OK:

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

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

在另一个测试中,我用卷曲的方法:

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 

*/ 

编辑.2:

由于某种原因,问题再次出现。

有没有办法解决这个问题?

+0

这个帖子被修改,因为这个问题必须被重构 –

一种解决方案是创建自定义策略插件并覆盖doApply方法,以删除Transfer-Encoding标头,如果它等于chunked使用ApiResponse对象。

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

并将新的自定义策略插件添加到您的服务。这是一个简单的tutorial来创建插件

+0

解决! \ o /非常感谢@ulab。 –

+0

很高兴!不用谢。 – ulab

+0

我不知道这是否是最好的事情。发生这种情况是因为我的服务没有在Response Header上传递Content-Length。这不是APIMan的问题。所以我必须在我的后端服务器 –