Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bug of header adding and getting in OpenFeign RequestInterceptor #1987

Closed
HaojunRen opened this issue Mar 21, 2023 · 8 comments
Closed

A bug of header adding and getting in OpenFeign RequestInterceptor #1987

HaojunRen opened this issue Mar 21, 2023 · 8 comments
Labels
feedback provided Feedback has been provided to the author

Comments

@HaojunRen
Copy link

HaojunRen commented Mar 21, 2023

Refer to the following issue at Spring Cloud community

spring-cloud/spring-cloud-openfeign#760

@vitalijr2
Copy link
Collaborator

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

@HaojunRen
Copy link
Author

It does not make sense, that means i should add double-quoters to every header value with json format one by one?

@vitalijr2
Copy link
Collaborator

vitalijr2 commented Apr 14, 2023

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.

@kdavisk6
Copy link
Member

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.

@kdavisk6 kdavisk6 added the feedback provided Feedback has been provided to the author label May 24, 2023
@yangyang122
Copy link

1699345955608

Can you add a switch here to enable parsing JSON data?

@yangyang122
Copy link

yangyang122 commented Nov 7, 2023

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.

@hyfsy
Copy link

hyfsy commented Dec 14, 2023

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

dvag-yannick-reifschneider added a commit to dvag-yannick-reifschneider/feign that referenced this issue Jul 10, 2024
This change adds a new method RequestTemplate#headerLiteral which allows adding headers which are not interpreted as Template expressions. This allows adding empty JSON objects to headers in RequestInterceptor implementations.

Fixes OpenFeign#2252, OpenFeign#1987
velo added a commit that referenced this issue Jul 10, 2024
* Allows adding literal header values to RequestTemplate

This change adds a new method RequestTemplate#headerLiteral which allows adding headers which are not interpreted as Template expressions. This allows adding empty JSON objects to headers in RequestInterceptor implementations.

Fixes #2252, #1987

* Update RequestTemplate.java

---------

Co-authored-by: Marvin <velo@users.noreply.github.com>
@kdavisk6
Copy link
Member

Fixed in #2471

velo added a commit that referenced this issue Oct 7, 2024
* Allows adding literal header values to RequestTemplate

This change adds a new method RequestTemplate#headerLiteral which allows adding headers which are not interpreted as Template expressions. This allows adding empty JSON objects to headers in RequestInterceptor implementations.

Fixes #2252, #1987

* Update RequestTemplate.java

---------

Co-authored-by: Marvin <velo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback provided Feedback has been provided to the author
Projects
None yet
Development

No branches or pull requests

5 participants