muuntaja icon indicating copy to clipboard operation
muuntaja copied to clipboard

Disabling Muuntaja with Content-Type header is case-sensitive

Open miikka opened this issue 5 years ago • 8 comments

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.

miikka avatar Apr 01 '19 11:04 miikka

I believe only request headers are downcased, response headers are sent as-is.

ikitommi avatar Apr 01 '19 13:04 ikitommi

Yes, so there's no established convention on which case should be used by the application and the middlewares to avoid exactly this situation.

miikka avatar Apr 01 '19 13:04 miikka

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?

ikitommi avatar Apr 01 '19 14:04 ikitommi

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.

miikka avatar Apr 01 '19 14:04 miikka

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....

ikitommi avatar Apr 01 '19 14:04 ikitommi

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

miikka avatar Apr 02 '19 08:04 miikka

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.

chrisbetz avatar Mar 17 '20 14:03 chrisbetz

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.

cch1 avatar Mar 05 '23 14:03 cch1