rockydgeekgod rockydgeekgod - 20 days ago 5
reST (reStructuredText) Question

Does Maru support only path params and not query params?

I was getting me feet wet with

elixir
and decided to use Maru as the RESTful framework. I went through the docs and googled for a day but could not find any documentation or examples on how to parse query params.

I want something like this

http://localhost:8080/app/rest/system/users?variable1=value1&variable2=value2


I want to parse the above url and populate a map with the query params
variable1
and
variable2
. Does Maru support this kind of operation?

Answer

I couldn't find any functionality to do this in Maru's source code, but since Maru uses the plug package internally and allows full access to customizing the plug pipeline, you can plug in Plug.Conn.fetch_query_params and then access the params using conn.params:

defmodule MyApp do
  use Maru.Router
  plug :fetch_query_params

  get do
    json(conn, conn.params)
  end
end
$ curl 'http://127.0.0.1:8800/?foo=bar&baz\[\]=quux'
{"foo":"bar","baz":["quux"]}