stratacast

stratacast

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.

Showing Posts 1 to 4

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 Plug.Parsers — Plug v1.20.2 to read the parameters from the body in a more structured way.

stratacast

stratacast OP

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:

BLUECALF

BLUECALF

Am asking what if I want to get some kind of Map so that I can get param details such as “username” and password in any http requests. So that I can use in backed.

The way am seeing at top, I get data as “bitsring” which I cannot use easily.

“name=John& password=opensesami” some string like that.

NobbZ

NobbZ

Looks like form encoded data, you should be able to use Plug.Conn.Query.decode/4 to decode it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews