joy-framework / joy

A full stack web framework written in janet

Home Page:https://joy.swlkr.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get query params on request

dalaidunc opened this issue · comments

Hey, nice project, just a small thing: If I send a request to a path like /home?name=something

It would be nice if the request handler could parse the query params into the map. I can see there is a :params struct which is empty, perhaps have another srtuct called :query for easy access to these things? e.g.

:query @{"name" "something"}

commented

It should be in :query-string probably not the best name now that you mention it, :query is better, nice and short

commented

Also if you want to print the request object you can use (printf "%q" request) to see what’s in there

There was no key :query-string for me. Also the (printf "%q" request) gave me this error in my terminal: compile error: unknown symbol “%q” on line 8, column 3 while compiling server.janet so I used the pp function instead. Not sure what version of joy I am using, but I just installed using jpm today.

commented

I should have been clearer, if you make your home.janet look like like this:

(import joy :prefix "")

(defn index [request]
  (printf "%q" request)

  [:div {:class "tc"}
   [:h1 "You found joy!"]
   [:p {:class "code"}
    [:b "Joy Version:"]
    [:span (string " " version)]]
   [:p {:class "code"}
    [:b "Janet Version:"]
    [:span janet/version]]])

Then you should be able to see the request hash map printed there with (printf "%q" request)

It seems to be the same output as using the pp command, but I have no keys for query-string. Perhaps it does not exist in the version of joy I have?

If I return all the keys on the request I get the following: @[:method :params :uri :headers]

commented

Alright so I put together a quick asciinema with joy 0.7.2 and janet 1.8.0 to test out the query-string middleware, hopefully I can get across what I'm trying to say better in a video and with all of the other joy stuff stripped away:

asciicast

Thanks for taking the time to do that. If I use your code exactly as you wrote it I do get back the query-string, but if I use my server code, there is no query-string available.

(import joy)

(defn home [request]
  (pp request)
  (pp (get request :params))
  (pp (get request :query-string))
  (pp (keys request))
  (pp (get request :uri))
  (printf "%q" request)
  (joy/render :text "You found joy!"))

(def routes [[:get "/" home] [:get "/home" home]])

(def app (joy/handler routes))

(joy/server app 8000)
commented

I should probably change the docs and emphasize the middleware more, each "feature" of joy is handled by middleware, so if you make this change you'll get the query string:

- (def app (joy/handler routes))
+ (def app (joy/query-string (joy/handler routes)))
commented

Another cool trick is you don't even have to run the http server, you can just call app as a function like this:

(app @{:method :get :uri "/home?hello=world"}) ; => @{:status 200 :body "You found joy!" ...}

Thanks for your patience, that works perfectly.