Fork me on GitHub
#ring
<
2018-06-14
>
ikitommi07:06:40

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

ikitommi07:06:02

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" "",
                    "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" "",
                    "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))

weavejester15:06:55

@ikitommi There’s a ring.util.request/content-type function.

weavejester15:06:42

Sorry, before I wrote ring.util.response/content-type when I meant ring.util.request/content-type.

ikitommi16:06:15

oh, thanks again! For some reason I haven’t ever used the .request ns. It does the trick indeed.

ikitommi16:06:31

;; 874ns
(cc/quick-bench
  (ring.util.request/content-type req))