I'm playing around with the plug router and trying to read the body of a simple post request in my router:
The form
<form action="/create_item" method="post">
<input type="text" value="whatever" name="name">
<input type="number" value="99" name="age">
<input type="submit">
</form>
My router
post("/create_item") do
{:ok, data, _conn} = read_body(conn)
send_resp(conn, 200, data)
end
If I submit my form, this will render name=input-value
to the page, but then I would have to parse that string to just get the value. This smells the the wrong way to do it.
From my little experience with elixir, it seems like I should be doing something like pattern-matching on the read_body(conn)
to pull out each key: value
from my form, but I cannot find anything about this in the plug docs. I tried digging through the phoenix source for ideas, but that's a bit beyond my elixir knowledge. Is there an obvious parse_form_data/1
function that I'm missing?