1

I've got NullPointerException trying to analyze the value of content type header returned in HttpResponse object. As it turns out, the case of the header name was different (i.e. 'content-type', but not 'Content-Type'), that's why HttpResponse object returns null instead of actual value. I'm really confused cause according to http specification header names are case-insensitive.

So I've created a test mock and below is a piece of code to reproduce the issue:

HttpRequest request = new HttpRequest();

request.setEndpoint('http://demo8399479.mockable.io/testHeaderCase');
request.setMethod('GET');

HttpResponse response = new Http().send(request);

System.debug('Get header content-type: ' + response.getHeader('content-type'));
System.debug('Get header Content-Type: ' + response.getHeader('Content-Type'));

And here is debug output:

20:59:12:131 USER_DEBUG [9]|DEBUG|Get header Content-Type: application/json; charset=UTF-8
20:59:12:131 USER_DEBUG [8]|DEBUG|Get header content-type: null

So, what's than the right way to fetch header by name ? Am I doing something wrong ? Why is it case-sensitive in apex ?

Thanks in advance for throwing daylight on this mystery.

2 Answers 2

2

Summer '15 Apex Reference (Page 1865):

Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct

You should use <Map>.keySet() and then compare key value using <String>.equalsIgnoreCase(string)

4
  • Mhm..., that's interesting. But I have no access to headers' map. I could only guess that inside HttpResponse object they are stored in the form of map. Why just response.getHeader doesn't handle this itself ? It definitely breaks http specification.
    – wesaw
    Jun 27, 2015 at 21:26
  • @wesaw but you have access to header keys by method getHeaderKeys
    – Ashwani
    Jun 27, 2015 at 22:17
  • ok, got it, thank you. It's an ugly workaround, but sounds like it's the only way to go)
    – wesaw
    Jun 27, 2015 at 22:45
  • There is nothing in the documentation of HttpResponse.getHeader to suggest that the implementation is done using a Map, so what the Salesforce documentation says about maps is not really relevant here. We should solely rely on the documentation on HttpResponse.getHeader itself, which, sadly, is silent on the topic of case-sensitivity.
    – Frans
    Feb 11, 2023 at 16:27
1

HttpResponse.getHeader(String) is broken and you shouldn't use it. As you found out, the header lookup is case-sensitive when it should be case-insensitive.

As @Ashwani alluded to, your only option at this point is to iterate over the header names returned by HttpResponse.getHeaderKeys() and compare those, ignoring case, to the name of the header you're looking for (with equalsIgnoreCase), then call getHeader with the key you found in the header keys list to get the header value.

I've raised this with Salesforce Support and they said they are aware of the issue and it is on the roadmap to be fixed, but they have no ETA at the moment. The relevant work items (these are internal, unfortunately we cannot see them) are W-8874377 and W-12113612.

If you have a support contract, I would urge you to raise a support ticket as well. The more people ask for this to be fixed, the more likely it is to be brought in scope for an upcoming release.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .