发送HTTP请求具有编码URI
问题描述:
我米试图如下发送HTTP请求,使用柑橘类测试框架http://www.citrusframework.org/发送HTTP请求具有编码URI
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编码值,但是当它通过柑橘发送请求时再次进行编码。 as tel%253A%252B94xxxxxxx
有没有办法正确发送编码的URI?
答
柑橘使用于Spring RestTemplate
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
...
}
URL被给定为字符串值和Spring将自动编码该值以下的方法。当传入一些已编码的字符串时,编码会执行两次。当使用非编码的字符串值时,Spring RestTemplate会应用uriVariables逻辑,这也会导致错误。
Citrus应该在使用URL对象而不是String值的RestTemplate上使用其他方法签名。作为一个临时的变通方法,您可以使用自定义RestTemplate子类覆盖这样的方法,并自动创建一个从字符串的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转换配置和引用豆在Citrus客户端组件上,使用属性rest-template。
您是否尝试将未编码的URI传递给Citrus?我想当Citrus和底层的Http客户端自动进行编码时,在测试代码中添加非编码的URI应该没问题。 –
HI Christoph,谢谢你的回复。是的,我尝试过。当发送非编码的URI时,它就像它的。例如tel:+ 94xxxxxxx(无编码)。但需要为服务器发送编码的URI。 –
Citrus在春天使用Rest模板,假设需要url编码,看起来像是“:”和“+”,它们忽略了编码的链接字符[link](https://docs.spring.io/spring/docs/current /javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-java.lang.String-org.springframework.http.HttpMethod-org.springframework.http.HttpEntity-java.lang.Class-java.lang 。目的...-) –