Skip to content
Ring middleware for request header parsing.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/ring/middleware
test/ring/middleware/test
.gitignore
LICENSE
README.md
project.clj

README.md

Ring middleware for parsing request headers.

It's common for many headers to have multiple values. Ring catenates the values intoa single string separated by commas. Wrap-request-headers allows you parse this string.

ring.middleware.request-headers/wrap-request-headers

(wrap-request-headers handler)
(wrap-request-headers handler parsing-rules)

The default behavior of wrap-request-headers is to split the header value string into a sequence. So a request map looking like:

{;...
 :headers {"accept" "text/html, application/xml; q=0.9"}
 ;...
}

will become:

{;...
 :headers {"accept" ("text/html" "application/xml; q=0.9")}
 ;...
}

Parsing

Many headers have their values augmented with extra parameters. Probably the most common one is the "q" parameter for denoting the "priority" of the value. Also in many cases the value is better represented as some other Clojure data type than string. To handle this, wrap-request-headers takes an optional map that contains parsing functions.

The map should be a mapping from header name to parsing function to be applied to every value of that header. If no parsing function is provided for certain header the values are left untouched.

If the wrap-request-headers was applied with the following as parsing-rules:

{"accept" (fn [s] (let [[media-range & params] (.split s ";")]
                    (if (empty? params)
                        media-range
                        [media-range params])))}

the request map:

{;...
 :headers {"accept" "text/html, application/xml; q=0.9"
           "accept-charset" "utf-8"}
 ;...
}

will become:

{;...
 :headers {"accept" ("text/html" ["application/xml" "q=0.9"])
           "accept-charset" "utf-8"}
 ;...
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.