A bug of header adding and getting in OpenFeign RequestInterceptor
Refer to the following issue at Spring Cloud community
https://github.com/spring-cloud/spring-cloud-openfeign/issues/760
Hi!
Please try to put keys to doublequoters, it works:
@Test
public void headerWithJSONValue() {
RequestTemplate template = new RequestTemplate()
.header("key", "{\"x\":1.0,\"y\":1.1}");
assertThat(template.headers()).hasSize(1);
assertThat(template.headers().keySet()).containsExactly("key");
assertThat(template.headers().get("key")).containsExactly("{\"x\":1.0,\"y\":1.1}");
}
It does not make sense, that means i should add double-quoters to every header value with json format one by one?
This is JSON's rule: string are wrapped double quotes, both in keys and values.
And as far as I guess this rule is used to avoid processing of header value, see Request Headers Expansion that uses curly brackets like JSON does.
It does not make sense, that means i should add double-quoters to every header value with json format one by one?
As @radio-rogal has stated, Feign supports expression based headers. This means you can use URI templates in your header values and have them resolved at execution time. The incompatibility you are seeing is related to the use of JSON as a header value, which conflicts with the URI Template specification where dynamic expressions are wrapped in braces {}.
If you chose to use Feign and require JSON based header values, you will need to quote them as discussed.
Can you add a switch here to enable parsing JSON data?
I try :
String data="{\"x\":1.0,\"y\":2.0}"; RequestTemplate rq = requestTemplate.header("v", data); Map<String, Collection<String>> headers = rq.headers(); headers.forEach((key, values) -> { System.out.println(key+"--"+values); });
result : v--[{"x":1.0,"y"}], but it's not the expected value.
Hi!
Please try to put keys to doublequoters, it works:
@Test public void headerWithJSONValue() { RequestTemplate template = new RequestTemplate() .header("key", "{\"x\":1.0,\"y\":1.1}"); assertThat(template.headers()).hasSize(1); assertThat(template.headers().keySet()).containsExactly("key"); assertThat(template.headers().get("key")).containsExactly("{\"x\":1.0,\"y\":1.1}"); }
It doesn't work in version: spring-cloud-starter-openfeign: 3.1.4 feign-core: 11.8