stratacast

stratacast

How to get POST data with Plug

I feel really dumb asking this, but I’ve bopped all over the internet, tried various things, and checked out some posts on this and I’m perplexed at why I can’t figure out how to simply retrieve data passed to my program via POST. Here is my Router module

defmodule Kahwiz.Router do
  use Plug.Router

  plug(Plug.Parsers, parsers: [:urlencoded])

  plug(:match)
  plug(:dispatch)

  get "/" do
    Kahwiz.IndexController.index(conn)
  end

  post "/" do
    # IO.inspect(conn.params, label: "The params")
    {:ok, data, _conn} = read_body(conn)
    IO.inspect(data, label: "The data")
    send_resp(conn, 200, "Got it #{data}")
  end

  match _ do
    send_resp(conn, 404, "oops")
  end
end

And my CURL command to send data to the server:
curl -X POST http://localhost:4001?param1=1234

In its current condition, data is empty, but on the command line I do get “Got it”. If you use the IO.inspect line to get the parameters passed to it, I see the data in the inspect function but also a large amount of errors:

The params: %{"param1" => "1234"}

20:59:47.224 [error] #PID<0.892.0> running Kahwiz.Router (connection #PID<0.891.0>, stream id 1) terminated
Server: localhost:4001 (http)
Request: POST /?param1=1234
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol String.Chars not implemented for %{"param1" => "1234"} of type Map. This protocol is implemented for the following type(s): Decimal, NaiveDateTime, Atom, Date, BitString, Float, Integer, List, Time, Version.Requirement, Version, URI, DateTime
        (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
        (elixir) lib/string/chars.ex:22: String.Chars.to_string/1
        (kahwiz) lib/router.ex:20: anonymous fn/2 in Kahwiz.Router.do_match/4
        (kahwiz) lib/plug/router.ex:259: Kahwiz.Router.dispatch/2
        (kahwiz) lib/router.ex:1: Kahwiz.Router.plug_builder_call/2
        (plug_cowboy) lib/plug/cowboy/handler.ex:12: Plug.Cowboy.Handler.init/2
        (cowboy) /usr/home/courtney/Documents/code/Elixir/kahwiz/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /usr/home/courtney/Documents/code/Elixir/kahwiz/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3

My goal in the end is to be able to submit the URL above and get “Got it 1234”

I’ve looked through the Plug docs on hexdocs but I’m having a hard time solving this. I’ve accomplished what I’m trying to do in the past with Phoenix but I’m trying to learn Elixir without a framework.

Marked As Solved

cmkarlsson

cmkarlsson

In your code you are trying to retrieve the body but you have not sent any body. The params are normal GET parameters which are retrieved another way. To post a body:

curl -X POST -d "param1=1234" http://localhost:4001

You can probably use Plug.Conn.fetch_query_params/2 to get the parameters from the query string rather than the body if that is what you are after.

You might also want to look into https://hexdocs.pm/plug/Plug.Parsers.html to read the parameters from the body in a more structured way.

Also Liked

stratacast

stratacast

You’re right. I’m dumb. I need to learn to IO.inspect more before I speak. I thought that conn.params contained the info initially and got nothing so I tried a variety things. It turned out to be the query_params as you suggest. In the end I changed to POST but I’m glad I learned a lot here and am remembering things. I’m picking up web development again after 3 years of stagnation and doing it with Elixir so I’m also drudging through remembering the intricacies of functional programming, since my only exposure to functional programming was using Racket 4 years ago :slight_smile:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement