Super simple Cowboy example throwing :badmap error

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