@weavejester thanks. There doesn’t seem to get get-content-type
available, the ring.util.response/get-header
returns the raw string, which might have stuff like encoding, e.g.`"application/json; encoding=utf-8"`. My use case is that I need it multiple times in a request pipeline for different purposes, I guess the current good practise is to parse it once (in a content-negotiation middleware) and push it to request so it doesn’t need to be reparsed for all users.
But well, just wanted to know the cause, I’ll wire together the libs using Muuntaja keys. Only gets parsed once and the reads are fast (despite stored under a library-spesific key):
;; Muuntaja pushes the parsed results into a Record for fast access
(defrecord RequestAndCharset [format charset])
;; a Sample request
(def req {:headers {"origin" "<http://localhost:3000>",
"host" "localhost:3000",
"user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
"content-type" "application/json",
"cookie" "JSESSIONID=fo2TyZwq24rKinCsfcHI_DgKz611PO9Lpyh1mLqf; _ga=GA1.1.1363736022.1528784471",
"content-length" "22",
"referer" "<http://localhost:3000/index.html>",
"connection" "keep-alive",
"accept" "application/json",
"accept-language" "en-US,en;q=0.9,fi-FI;q=0.8,fi;q=0.7",
"accept-encoding" "gzip, deflate, br",
"dnt" "1"}
:muuntaja/request (->RequestAndCharset "application/json" nil)
:muuntaja/response (->RequestAndCharset "application/json" nil)})
;; 352ns
(cc/quick-bench
(ring.util.response/get-header req "content-type"))
;; 45ns
(cc/quick-bench
(-> req :headers (get "content-type")))
;; 13ns
(cc/quick-bench
(-> req :muuntaja/request :format))
@ikitommi There’s a ring.util.request/content-type
function.
Sorry, before I wrote ring.util.response/content-type
when I meant ring.util.request/content-type
.
oh, thanks again! For some reason I haven’t ever used the .request
ns. It does the trick indeed.
;; 874ns
(cc/quick-bench
(ring.util.request/content-type req))