gaggle

gaggle

Super simple Cowboy example throwing :badmap error

Hey all,

Maybe my tired eyes are just tiredly overlooking a tired tiny detail, but for all the staring and experimenting I’ve done I can’t seem to get past this :sweat_smile:

I start iex and start my cowboy module:

$ iex -S mix
Erlang/OTP 26 [erts-14.2.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [dtrace]

Interactive Elixir (1.16.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Cowboy.start
Cowboy server started on port 8080
:ok

Then I curl in another terminal:

$ curl http://localhost:8080

And the server throws this:

21:58:22.288 [error] Ranch listener :my_http_listener, connection process #PID<0.247.0>, stream 1 had its request process #PID<0.248.0> exit with reason {:badmap, [{"content-type", "text/html"}]} and stacktrace [{:cowboy_req, :reply, 4, [file: ~c"/cowboy_example/deps/cowboy/src/cowboy_req.erl", line: 837]}, {HelloHandler, :init, 2, [file: ~c"lib/application.ex", line: 19]}, {:cowboy_handler, :execute, 2, [file: ~c"/cowboy_example/deps/cowboy/src/cowboy_handler.erl", line: 37]}, {:cowboy_stream_h, :execute, 3, [file: ~c"/cowboy_example/deps/cowboy/src/cowboy_stream_h.erl", line: 306]}, {:cowboy_stream_h, :request_process, 3, [file: ~c"/cowboy_example/deps/cowboy/src/cowboy_stream_h.erl", line: 295]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]

Headers are “:badmap”?

Here’s the sourcecode:

defmodule Cowboy do
  def start() do
    dispatch = :cowboy_router.compile([ {:_, [ {"/", HelloHandler, []} ]} ])

    {:ok, _} = :cowboy.start_clear(
        :my_http_listener,
        [{:port, 8080}],
        %{env: %{dispatch: dispatch}}
      )

    IO.puts("Cowboy server started on port 8080")
  end
end

defmodule HelloHandler do
  def init(req, _opts) do
    {:ok, resp} = :cowboy_req.reply( 200, [{"content-type", "text/html"}], "<h1>Hello World!</h1>", req )
    {:ok, resp, :nostate}
  end
end

I must be doing something wrong but I’m pretty new to navigating Erlang docs and translating it into Elixir… can you spot it, or help me spot it?

(And mix.exs is configured with {:cowboy, "~> 2.11"} in deps, and extra_applications: [:logger, :cowboy] in application FWIW)

Marked As Solved

gaggle

gaggle

Ah… ah yes!, moving to a map for headers solves that error. I tried that early but apparently got stuck on another error after without realizing, and never went back to try that. Thanks for suggesting it.

nine nines docs show this:

init(Req0=#{method := <<"GET">>}, State) ->
    Req = cowboy_req:reply(200, #{
        <<"content-type">> => <<"text/plain">>
    }, <<"Hello world!">>, Req0),
    {ok, Req, State};

In hindsight I just had to search to confirm #{} syntax in Erlang means a map in Elixir, d’oh. And I misunderstood what comes back from cowboy_req.reply, now that I can navigate Erlang code a tiny bit I can see it’s init that returns an ok-tuple, not .reply itself.

Anyway, everything works now, thanks for the assist @Sanjibukai!

Updated code

defmodule CowboyExample.Application do
use Application

def start(_type, args) do
dispatch = :cowboy_router.compile([{:
, [{“/”, CowboyExample.HelloHandler, }]}])

{:ok, _} =
  :cowboy.start_clear(
    :my_http_listener,
    [{:port, 8080}],
    %{env: %{dispatch: dispatch}}
  )

IO.puts("Cowboy server started on port 8080")

Supervisor.start_link([], strategy: :one_for_one, name: CowboyExample.Supervisor)

end
end

defmodule CowboyExample.HelloHandler do
def init(req, _opts) do
req = :cowboy_req.reply(200, %{“content-type” => “text/html”}, “

Hello World!

”, req)
{:ok, req, :nostate}
end
end

Also Liked

Sanjibukai

Sanjibukai

If I look here, it seems the second argument to cowboy_req.reply should be a map (not a tuple) Nine Nines: The Req object

Maybe, try using a map %{"content-type" => "text/html"}

Although I don’t have much experience with bare cowboy…

Also, I don’t remember if we need to use single quotes for string or not (for the Erlang side)…

Good luck…

Edit: According to the above link, the maps fields is expected to be “binaries” and according to here Erlang/Elixir Syntax: A Crash Course - The Elixir programming language using double quotes on the Elixir side is enough…

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement