mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Fix echo_post example
This commit is contained in:
parent
1470f88319
commit
4ced1d0994
3 changed files with 31 additions and 9 deletions
|
@ -16,9 +16,9 @@ start(_Type, _Args) ->
|
|||
{"/", toppage_handler, []}
|
||||
]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
|
||||
{env, [{dispatch, Dispatch}]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
|
||||
env => #{dispatch => Dispatch}
|
||||
}),
|
||||
echo_post_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
|
|
|
@ -14,16 +14,19 @@ init(Req, Opts) ->
|
|||
maybe_echo(<<"POST">>, true, Req) ->
|
||||
{ok, PostVals, Req2} = cowboy_req:body_qs(Req),
|
||||
Echo = proplists:get_value(<<"echo">>, PostVals),
|
||||
echo(Echo, Req2);
|
||||
echo(Echo, Req2),
|
||||
Req2;
|
||||
maybe_echo(<<"POST">>, false, Req) ->
|
||||
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
|
||||
cowboy_req:reply(400, [], <<"Missing body.">>, Req),
|
||||
Req;
|
||||
maybe_echo(_, _, Req) ->
|
||||
%% Method not allowed.
|
||||
cowboy_req:reply(405, Req).
|
||||
cowboy_req:reply(405, Req),
|
||||
Req.
|
||||
|
||||
echo(undefined, Req) ->
|
||||
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
||||
echo(Echo, Req) ->
|
||||
cowboy_req:reply(200, [
|
||||
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
|
||||
], Echo, Req).
|
||||
cowboy_req:reply(200, #{
|
||||
<<"content-type">> => <<"text/plain; charset=utf-8">>
|
||||
}, Echo, Req).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue