muuntaja
muuntaja copied to clipboard
Disabling Muuntaja with Content-Type header is case-sensitive
Muuntaja does not encode the response if a Content-Type
header has been set. Header names are case-insensitive but Muuntaja's check is case-sensitive, so if you set the header content-type
, it won't disable the encoding.
Considering that the Ring spec says that the request headers will be downcased, I'd expect downcased headers to work in the response maps as well.
I believe only request headers are downcased, response headers are sent as-is.
Yes, so there's no established convention on which case should be used by the application and the middlewares to avoid exactly this situation.
Hmm. The http spec says it's "Content-Type" and (all?) the clojure libs (ring, pedestal, yada, muuntaja) use this casing as well. I would say it's the convention to use.
https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/util/response.clj
I wonder if there any fast ways to do ignore-case lookup on pers.maps?
I don't know if there's a fast way. I suppose that ultimately this is a bug in the Ring spec, since it does not specify how to handle response headers in an interoperable way, but probably it's too late to change it now.
I recall James thinked aloud about Ring Spec2, with namespaces keys. So, maybe there is a chance to get something for this too? I would like to see Ring Requests and Responses as protocols myself....
I guess Ring's answer to this problem right now is that you should use find-header
/get-header
/update-header
: https://github.com/ring-clojure/ring/blob/1d858fb13381e92f5ced4dedf42853fc062ace8c/ring-core/src/ring/util/response.clj#L186-L209
Hi, working on that. Just to let you know. I'll prepare a PR as soon as I'm done. You might want to check out my fork in the meanwhile.
I'm using muuntaja with babashka/http-client and have run into this same problem with a server that returns "content-type" (lower-case).
(I'm considering hato, but am nervous about the lack of activity on that repo)
This is an awesome library and it's ability to work consistently for clients and servers, for requests and responses is refreshing. Thank you.
FWIW, I think the "safe" way to handle headers is something very much like the ring code referenced above. Another option would be to normalize the headers before processing using camel-snake-kebab
.