牛仔 POST 交易
Cowboy POST handler
正在尝试接收 POST 请求并将其存储到 ETS table
这是代码
init(Req0, Opts) ->
Method = cowboy_req:method(Req0),
HasBody = cowboy_req:has_body(Req0),
Req = maybe_echo(Method, HasBody, Req0),
{ok, Req, Opts}.
maybe_echo(<<"POST">>, true, Req0) ->
{ok, PostVals, Req} = cowboy_req:read_urlencoded_body(Req0),
Echo = proplists:get_value(<<"echo">>, PostVals),
echo(Echo, Req);
maybe_echo(<<"POST">>, false, Req) ->
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
maybe_echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
echo(undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(Echo, Req) ->
Inf = #news{id=25, created=today, article=Echo},
case ets:insert(news, {Inf#news.id, Inf#news.created, Inf#news.article}) of
true -> cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain; charset=utf-8">>}, Echo, Req);
_ ->
Error = <<"{\"error\": \"error\"}">>,
cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain; charset=utf-8">>}, Error, Req)
end.
当我卷曲时:
$ curl -i -H "Content-Type: application/json" -X POST -d echo='{"action":"insert","key":"some_key", "value":[1,2,3]}' http://localhost:8080/
我遇到错误:
=错误报告==== 29-Jan-2017::18:57:21 ===
Ranch 侦听器 http,连接进程 <0.240.0>,流 1 的请求进程 <0.241.0> 退出,原因是 badarg 和堆栈跟踪 [{ets,insert,[news,{25,today,<<"{\"action\":\"insert\",\"key\":\"some_key\",\"value\":[1,2,3]}">>}],[]}, {post_handler,echo,2,[{file,"e:/_dev/news/_build/default/lib/news/src/post_handler.erl"},{line,25}]},{post_handler,init,2,[{file,"e:/_dev/news/_build/default/lib/news/src/post_handler.erl" },{line,8}]},{cowboy_handler,execute,2,[{file,"e:/_dev/news/_build/default/lib/cowboy/src/cowboy_handler.erl"},{line,39}]},{cowboy_stream_h,execute,3 ,[{file,"e:/_dev/news/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},{line,173}]},{cowboy_stream_h,proc_lib_hack,3,[{file,"e:/_dev/news/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},{line,158 }]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]
但是当我这样使用 echo 时:
echo(Echo, Req) ->
cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain; charset=utf-8">>}, Echo, Req)
end.
我收到请求 - ({"action":"insert","key":"some_key","value":[1,2,3]} )
ETS 似乎有什么问题?但我不知道我哪里搞砸了
在其他模块中创建 ets
ets:new(新闻, [ordered_set, protected, named_table, {keypos,1}, {read_concurrency, true}, {write_concurrency, true }])
你能告诉我解决这个问题的正确方向吗
ets:new
调用中的 protected
选项意味着只有创建 ETS table 的进程才能 insert
数据。其他进程只能读取数据。
改为使用 public
,所有进程都将具有 read/write 访问权限。
正在尝试接收 POST 请求并将其存储到 ETS table
这是代码
init(Req0, Opts) ->
Method = cowboy_req:method(Req0),
HasBody = cowboy_req:has_body(Req0),
Req = maybe_echo(Method, HasBody, Req0),
{ok, Req, Opts}.
maybe_echo(<<"POST">>, true, Req0) ->
{ok, PostVals, Req} = cowboy_req:read_urlencoded_body(Req0),
Echo = proplists:get_value(<<"echo">>, PostVals),
echo(Echo, Req);
maybe_echo(<<"POST">>, false, Req) ->
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
maybe_echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
echo(undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(Echo, Req) ->
Inf = #news{id=25, created=today, article=Echo},
case ets:insert(news, {Inf#news.id, Inf#news.created, Inf#news.article}) of
true -> cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain; charset=utf-8">>}, Echo, Req);
_ ->
Error = <<"{\"error\": \"error\"}">>,
cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain; charset=utf-8">>}, Error, Req)
end.
当我卷曲时:
$ curl -i -H "Content-Type: application/json" -X POST -d echo='{"action":"insert","key":"some_key", "value":[1,2,3]}' http://localhost:8080/
我遇到错误:
=错误报告==== 29-Jan-2017::18:57:21 === Ranch 侦听器 http,连接进程 <0.240.0>,流 1 的请求进程 <0.241.0> 退出,原因是 badarg 和堆栈跟踪 [{ets,insert,[news,{25,today,<<"{\"action\":\"insert\",\"key\":\"some_key\",\"value\":[1,2,3]}">>}],[]}, {post_handler,echo,2,[{file,"e:/_dev/news/_build/default/lib/news/src/post_handler.erl"},{line,25}]},{post_handler,init,2,[{file,"e:/_dev/news/_build/default/lib/news/src/post_handler.erl" },{line,8}]},{cowboy_handler,execute,2,[{file,"e:/_dev/news/_build/default/lib/cowboy/src/cowboy_handler.erl"},{line,39}]},{cowboy_stream_h,execute,3 ,[{file,"e:/_dev/news/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},{line,173}]},{cowboy_stream_h,proc_lib_hack,3,[{file,"e:/_dev/news/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},{line,158 }]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]
但是当我这样使用 echo 时:
echo(Echo, Req) ->
cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain; charset=utf-8">>}, Echo, Req)
end.
我收到请求 - ({"action":"insert","key":"some_key","value":[1,2,3]} )
ETS 似乎有什么问题?但我不知道我哪里搞砸了
在其他模块中创建 ets
ets:new(新闻, [ordered_set, protected, named_table, {keypos,1}, {read_concurrency, true}, {write_concurrency, true }])
你能告诉我解决这个问题的正确方向吗
ets:new
调用中的 protected
选项意味着只有创建 ETS table 的进程才能 insert
数据。其他进程只能读取数据。
改为使用 public
,所有进程都将具有 read/write 访问权限。