I'm fetching HTTP headers with Spring's RestTemplate.
HTTP headers are case insensitive, but the documentation for HttpHeaders doesn't seem to acknowledge this.
A quick test shows that things are working as expected...
HttpHeaders headers = restTemplate.headForHeaders(url);
Long a = Long.parseLong(headers.getFirst("Content-Length"));
Long b = Long.parseLong(headers.getFirst("content-length"));
assert( a.equals(b) ); // passes
Can I be sure that this test will pass under all Spring configurations?
Per this commit it is explicitly stated that HttpHeaders are case-insensitive:
Note that
HttpHeadersgenerally treats header names in a case-insensitive manner.
Old answer
HttpHeaders has only one public constructor and its body:
public HttpHeaders() {
this(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH), false);
}
And according to LinkedCaseInsensitiveMap docs:
LinkedHashMap variant that stores String keys in a case-insensitive manner, for example for key-based access in a results table.
Preserves the original order as well as the original casing of keys, while allowing for contains, get and remove calls with any case of key.
So yes, it always works in case insensitive manner.
But why you do not use HttpHeaders#getContentLength()? :
// no need to convert String to long
long contentLength = httpHeaders.getContentLength();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With