发送带有编码URI的http请求

时间:2017-03-31 11:49:55

标签: citrus-framework

我尝试使用柑橘测试框架http://www.citrusframework.org/

按如下方式发送http请求
http().client(ENV).post("/xx/v1/ouxxtbound/tel%3A%2B94xxxxxxx")
                .header("Authorization", authorization)**strong text**
                .header("Accept", "application/json")
                .payload(send)
                .contentType("application/json");

它正在传递一个url编码值,但当它在Citrus.as发送请求时再次编码时,电话号码为%253A%252B94xxxxxxx

是否有正确发送编码URI的方法?

1 个答案:

答案 0 :(得分:0)

Citrus在Spring RestTemplate上使用以下方法

public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
    ...
}

url以String值的形式给出,Spring将自动编码该值。传入一些已编码的String时,编码完成两次。当使用非编码的String值时,Spring RestTemplate会应用uriVariables逻辑,这也会导致错误。

Citrus应该在RestTemplate上使用一些其他方法签名,它使用URL对象而不是String值。作为临时解决方法,您可以使用自定义RestTemplate子类来覆盖此类方法,并自动从String创建URL对象:

@Override
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
    return super.exchange(URI.create(url), method, requestEntity, responseType);
}

您可以将自定义RestTemplate子类作为Spring bean添加到配置中,并使用属性 rest-template 在Citrus客户端组件上引用bean。