What's in the Luminus Request Object?
Table of Contents
If you use the luminus framework, you will be using the request object for things like reading the query string or the session.
However it doesn't seem to be documented anywhere, what entirely is inside this object.
1 Let's take a look
Here is an example of that variable's contents from a test app at
http://localhost:3000/dumpreq?foo=bar&sun=bright#potato
with post
data "color=red&vehicle=car"
:
{:reitit.core/match #reitit.core.Match{:template "/dumpreq", :data {:middleware [#function[todefer.middleware/wrap-formats]], :post {:handler #function[todefer.routes.home/dumpreq-page]}}, :result #reitit.ring.Methods{:get nil, :head nil, :post #reitit.ring.Endpoint{:data {:middleware [#function[todefer.middleware/wrap-formats]], :handler #function[todefer.routes.home/dumpreq-page]}, :handler #function[todefer.middleware/wrap-formats/fn--10196], :path "/dumpreq", :method :post, :middleware [#reitit.middleware.Middleware{:name nil, :wrap #function[todefer.middleware/wrap-formats], :spec nil}]}, :put nil, :delete nil, :connect nil, :options #reitit.ring.Endpoint{:data {:middleware [#function[todefer.middleware/wrap-formats]], :no-doc true, :handler #function[reitit.ring/fn--15549/fn--15558]}, :handler #function[todefer.middleware/wrap-formats/fn--10196], :path "/dumpreq", :method :options, :middleware [#reitit.middleware.Middleware{:name nil, :wrap #function[todefer.middleware/wrap-formats], :spec nil}]}, :trace nil, :patch nil}, :path-params {}, :path "/dumpreq"}, :reitit.core/router #object[reitit.core$lookup_router$reify__15157 0x659f660a "reitit.core$lookup_router$reify__15157@659f660a"], :protocol "HTTP/1.1", :cookies {}, :remote-addr "0:0:0:0:0:0:0:1", :params {:color "red", :vehicle "car", :foo "bar", :sun "bright"}, :flash nil, :headers {"accept" "*/*", "content-length" "19", "content-type" "application/x-www-form-urlencoded", "user-agent" "curl/7.74.0", "host" "localhost:3000"}, :server-port 3000, :muuntaja/request #FormatAndCharset{:format nil, :charset "utf-8", :raw-format "application/x-www-form-urlencoded"}, :content-length 19, :form-params {"color" "red", "vehicle" "car"}, :websocket? false, :server-exchange #object[io.undertow.server.HttpServerExchange 0x46c5aee1 "HttpServerExchange{ POST /dumpreq}"], :query-params {"foo" "bar", "sun" "bright"}, :content-type "application/x-www-form-urlencoded", :character-encoding "ISO-8859-1", :context "", :uri "/dumpreq", :server-name "localhost", :query-string "foo=bar&sun=bright", :path-params {}, :muuntaja/response #FormatAndCharset{:format "application/json", :charset "utf-8", :raw-format "*/*"}, :body #object[io.undertow.io.UndertowInputStream 0x2837271d "io.undertow.io.UndertowInputStream@2837271d"], :multipart-params {}, :scheme :http, :request-method :post, :session nil}
2 Examples
Here are some useful examples of bits you might want to access:
2.1 A specific query-string parameter:
(-> request :query-params (get "editme"))
Note that I do recommend using threading as it allows you to chain
extra steps, such as to convert it to a number with parse-long
.
2.2 Data sent by POST:
(-> request :params :editme)
2.3 Extracted path components
Luminus routing can extract parts of the path for you.
This will be in path-params
(-> request :path-params :editme)
2.4 Full path
If you want the full path including query-string:
(str (request :uri) "?" (request :query-string))
2.5 Session:
(request :session)
Please read the Luminus session docs
2.6 File upload
(request :multipart-params)
It is actually documented Handling file uploads
3 More tips
Encoding things:
(defn urlencode [foo] (java.net.URLEncoder/encode foo "UTF-8"))