7

I am developing a java based web application in spring-boot where I am sending http-header from server to client end.

Server-side I have used Spring-boot in form of REST API, whereas at client end we have simple plain HTML5/Angular framework.

My query is, while I am sending any header from server then at client end I always get it into lowercase vs actual. For example, I am setting header something like,

header.add("KK-ACTUAL-VALUE", "sfsfjDFFDHTsdfJKKA");

At client end, it gives,

kk-actual-value : "sfsfjDFFDHTsdfJKKA"; (Header name converts into lower case!)

Now, the question is, how to prevent it ?

I want to have the same header name what is actually passed by Server/API.

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
  • Header names should be case insensitive, does it matter ? https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive – Ori Marko Mar 14 '19 at 08:49
  • 1
    As request headers should be, by protocol definition, case insensitive, I don't think it's a good solution to design your app behaviour against it. – Matteo Baldi Mar 14 '19 at 08:50
  • @MatteoBaldi that's right. but there is a specific requirement where I need to implement it. – Vishal Gajera Mar 14 '19 at 08:52
  • @user7294900 I know this is quite odd requirement but this is what I get & need to implement it somehow. – Vishal Gajera Mar 14 '19 at 08:54
  • I am adding custom headers to my response and Spring boot doesn't convert headers to lowercase in my case. Have you checked response headers by making API call from tools like Postman? – Harshit Mar 14 '19 at 09:03
  • `header.add("KK-ACTUAL-VALUE", "sfsfjDFFDHTsdfJKKA");` - which exact class is header? – Selaron Mar 14 '19 at 09:25
  • It's a VERY bad idea to have casing in header names, because while header names are technically case-insensitive... they **MUST** be converted to lower-case to be HTTP/2 compliant according to HTTP/2 RFC spec. Any header name that is not lower-cased is considered malformed, so as far as I'm concerned tomcat is doing you a favor here. – AnorZaken Jul 18 '22 at 08:45

2 Answers2

9

Looks like tomcat container turns the headers to lowercase. Since spring boot uses an embedded tomcat is been affected by it. More info here: https://github.com/mitre/HTTP-Proxy-Servlet/issues/65

As the Http standard specifies:

Each header field consists of a case-insensitive field name followed by a colon (":") ...

https://www.rfc-editor.org/rfc/rfc7230#section-3.2

Community
  • 1
  • 1
Marco Capo
  • 226
  • 3
  • 7
  • Header names must be lower-cased before encoding to be HTTP/2 compliant anyway: [https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2](https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2) – AnorZaken Jul 18 '22 at 08:55
  • `Just as in HTTP/1.x, header field names are strings of ASCII characters that are compared in a case-insensitive fashion. However, header field names MUST be converted to lowercase prior to their encoding in HTTP/2. A request or response containing uppercase header field names MUST be treated as malformed (Section 8.1.2.6).` – AnorZaken Jul 18 '22 at 08:56
-1

To make the header case-sensitive you can set it to the MultiValueMap, and then pass it as a parameter to the HttpHeaders constructor.

The code snippet can be something like this:

 MultiValueMap <String, String> headers = new LinkedMultiValueMap<>();
    headers.add("KK-ACTUAL-VALUE", "sfsfjDFFDHTsdfJKKA");
    HttpHeaders responseHeaders = new HttpHeaders(headers);

enter image description here

Ivan Polovyi
  • 94
  • 3
  • 5