Rest 牛仔多方法处理程序
试图通过Cowboy设置restful API 我需要做的主要事情是对所有方法使用一个处理程序 这是路由器::Rest 牛仔多方法处理程序,rest,api,erlang,handler,cowboy,Rest,Api,Erlang,Handler,Cowboy,试图通过Cowboy设置restful API 我需要做的主要事情是对所有方法使用一个处理程序 这是路由器:: start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ {"/api", handler, []}, {"/api/:id", handler, []} ]} ]), {ok, _} = cowboy
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/api", handler, []},
{"/api/:id", handler, []}
]}
]),
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
env => #{dispatch => Dispatch}
}),
api_sup:start_link().
下面是处理程序代码::
-module(handler).
-export([init/3, handle/2]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, Opts) ->
case cowboy_req:method(Req) of
<<"POST">> ->
Body = cowboy_req:has_body(Req),
Request = postMethod(<<"POST">>, Body, Req),
{ok, Request, Opts};
<<"GET">> ->
#{id := Id} = cowboy_req:match_qs([{id, [], undefined}], Req),
Request = getMethod(<<"GET">>, Id, Req),
{ok, Request, Opts};
<<"PUT">> ->
Body = cowboy_req:has_body(Req),
Request = putMethod(<<"PUT">>, Body, Req),
{ok, Request, Opts};
<<"DELETE">> ->
#{id := Id} = cowboy_req:match_qs([{id, [], undefined}], Req),
Request = deleteMethod(<<"DELETE">>, Id, Req),
{ok, Request, Opts}
end.
postMethod(<<"POST">>, _Body, Req) ->
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json; charset=utf-8">>}, <<"{\"status\": \"POST\"}">>, Req).
getMethod(<<"GET">>, _Id, Req) ->
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json; charset=utf-8">>}, <<"{\"status\": \"GET\"}">>, Req).
putMethod(<<"PUT">>, _Body, Req) ->
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json; charset=utf-8">>}, <<"{\"status\": \"PUT\"}">>, Req).
deleteMethod(<<"DELETE">>, _Id, Req) ->
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json; charset=utf-8">>}, <<"{\"status\": \"DELETE\"}">>, Req).
-模块(处理程序)。
-导出([init/3,handle/2])。
初始化(_传输,请求,[])->
{确定,请求,未定义}。
句柄(请求、选项)->
案例牛仔需求:方法(需求)
->
车身=牛仔要求:有车身(要求),
请求=方法后(,主体,请求),
{ok,Request,Opts};
->
#{id:=id}=cowboy_-req:match_-qs([{id,[],未定义的}],req),
请求=getMethod(,Id,Req),
{ok,Request,Opts};
->
车身=牛仔要求:有车身(要求),
请求=putMethod(,Body,Req),
{ok,Request,Opts};
->
#{id:=id}=cowboy_-req:match_-qs([{id,[],未定义的}],req),
请求=删除方法(,Id,Req),
{好的,请求,选择}
结束。
方法后(,_Body,Req)->
牛仔请求:回复(200,{=>},请求)。
getMethod(,_Id,Req)->
牛仔请求:回复(200,{=>},请求)。
putMethod(,_Body,Req)->
牛仔请求:回复(200,{=>},请求)。
deleteMethod(,_Id,Req)->
牛仔请求:回复(200,{=>},请求)。
我得到了错误:牛仔500错误,因为
{Method,Req1}=cowboy_-req:Method(Req0)
是一个元组,类似于{,}
,而不是二进制
找到了答案。
下面是运行良好的代码(使用Cowboy的master verison)
-模块(处理程序)。
-导出([init/2])。
-导出([content\u types\u provided/2])。
-导出([content\u types\u accepted/2])。
-导出([允许的方法/2])。
-导出([router/2])。
初始化(请求,选择)->
{牛仔休息,请求,选择}。
允许的方法(请求、状态)->
{[,],请求,状态}。
提供的内容类型(请求、状态)->
{[{,路由器}],请求,状态}。
内容类型接受(请求、状态)->
{[{,路由器}],请求,状态}。
路由器(请求、选择)->
案例牛仔需求:方法(需求)
->
{,Req,State};
->
{,Req,State};
->
{,Req,State};
->
{,请求,状态}
结束。
您还可以在content\u types\u accepted/2回调方法中添加路由器逻辑,如下所示:
content_types_accepted(Req, State) ->
case cowboy_req:method(Req) of
{<<"POST">>, _ } ->
Accepted = {[{<<"application/json">>, post_json}], Req, State};
{<<"PUT">>, _ } ->
Accepted = {[{<<"application/json">>, put_json}], Req, State}
end,
Accepted.
content\u types\u accepted(请求、状态)->
案例牛仔需求:方法(需求)
{, _ } ->
已接受={[{,post_json}],请求,状态};
{, _ } ->
已接受={[{,put_json}],请求,状态}
完,,
认可的。
我认为通过这种方式,您可以为不同的HTTP谓词/方法使用单独的处理程序。这也为您提供了更清晰的代码:)我采用了一种稍微不同的方法。我使用函数,而不是使用
case
,模式匹配请求中的方法,然后使用不支持的代码405方法捕获所有内容
-module(handler).
-export([init/2]).
init(Req0, State) ->
Req = case api:validate_headers(api_key, Req0) of
ok -> handle(Req0, State);
{error, Error} -> api:reply_failure(403, Error, Req0)
end,
{ok, Req, State}.
handle(Req0=#{method := <<"GET">>}, _) ->
Data = "...", % Some data goes here
api:reply_success([{<<"data">>, Data}], Req0);
handle(Req0=#{method := <<"POST">>}, _) ->
Data = "...", % Some data goes here
api:reply_success([{<<"data">>, Data}], Req0);
handle(Req=#{method := <<"OPTIONS">>}, _) ->
api:reply_options(Req);
handle(Req, _) ->
cowboy_req:reply(405, Req).
-模块(处理程序)。
-导出([init/2])。
初始化(请求0,状态)->
Req=案例api:验证
确定->处理(需求0,状态);
{error,error}->api:reply_失败(403,error,Req0)
完,,
{ok,Req,State}。
句柄(请求0=#{method:=},#)->
Data=“…”,%有些数据放在这里
api:reply_success([{,Data}],请求0);
句柄(请求0=#{method:=},#)->
Data=“…”,%有些数据放在这里
api:reply_success([{,Data}],请求0);
句柄(请求=#{方法:=},#)->
api:回复选项(Req);
句柄(请求)->
牛仔要求:回复(405,要求)。
您启动此服务器的终端中是否有任何错误?没有,启动服务器时一切正常从api_app.app中删除了lager,连接错误消失。但现在我面临500个错误是的,我可以在用户手册中看到,但我不明白为什么它是牛仔echo_post示例中的
:[这个问题对于99s团队来说,使用tuple{Method,Req1}=cowboy_req:Method(Req0)--它是针对1.1.x版本的。主版本现在使用二进制
-module(handler).
-export([init/2]).
init(Req0, State) ->
Req = case api:validate_headers(api_key, Req0) of
ok -> handle(Req0, State);
{error, Error} -> api:reply_failure(403, Error, Req0)
end,
{ok, Req, State}.
handle(Req0=#{method := <<"GET">>}, _) ->
Data = "...", % Some data goes here
api:reply_success([{<<"data">>, Data}], Req0);
handle(Req0=#{method := <<"POST">>}, _) ->
Data = "...", % Some data goes here
api:reply_success([{<<"data">>, Data}], Req0);
handle(Req=#{method := <<"OPTIONS">>}, _) ->
api:reply_options(Req);
handle(Req, _) ->
cowboy_req:reply(405, Req).