softwaremill / sttp

The Scala HTTP client you always wanted!

Home Page:https://sttp.softwaremill.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is `null` valid header field value?

wenhoujx opened this issue · comments

hello , I recently debugged an issue where i passed an expression evaluated as null as sttp header value. Some in the lines of the following:

basicRequest.header("name", map.getOrNull("field"))

Netty DefaultHeaders.java throws NullPointerException: Value error.

Reading http/1.1 and http/2 spec it seems the header field value must be text string, even if empty.

Is it explicitly called out somewhere in sttp docs that null should not be passed in as a header field value? Thanks!

null values will probably either not work, or throw NullPointerException throughout sttp and other Scala libraries, as null is very rarely used (if at all) when writing Scala code. Alternatively, you can use an Option, which is very commonly used. So, your example would become basicRequest.header("name", map.get("field"))

@adamw thanks for the reply. it makes sense most scala libs use Option instead of risking having NPE.
Occasionally we use java class in scala and some outdated conventions may introduce null.
Our case was we get the http request header from log4j ThreadContext.get("correlction-id") , which returns null if missing.

Do you think it's the library caller's responsibility to check the null or is should be done in the library?

I very rarely see any questions / bug reports concerning nulls, so I think if you're interfacing with Java it's mainly the responsiblity of the wrapper layer - in this case, that would be in your app. You can always do Option(ThreadContext.get(...)) which correctly handles null results

sound good, thanks for the answers.

No problem, I'll close this then :)