3

我开始学习 Erlang。我想写一个简单的基于牛仔的 HTTP 服务器,它可以接收通过 HTTP POST 发送的文件。所以我创建了简单的处理程序:

-module(handler).
-behaviour(cowboy_http_handler).
-export([init/3,handle/2,terminate/3]).

init({tcp, http}, Req, _Opts) ->
  {ok, Req, undefined_state}.

handle(Req, State) ->
  Body = <<"<h1>Test</h1>">>,
  {ok, Req2} = cowboy_req:reply(200, [], Body, Req),
  {ok, Req2, State}.

terminate(_Reason, _Req, _State) ->
  ok.

此代码可以处理 GET 请求。但是如何处理 HTTP POST 请求?

4

4 回答 4

8

您的代码使用任何 HTTP 方法处理请求。如果要处理特定的 HTTP 请求方法,则必须在回调句柄/2 中测试方法名称。在这里你可以看到一个简单的例子:

handle(Req, State) ->
    {Method, Req2} = cowboy_req:method(Req),
    case Method of
        <<"POST">> ->
            Body = <<"<h1>This is a response for POST</h1>">>;
        <<"GET">> ->
            Body = <<"<h1>This is a response for GET</h1>">>;
        _ ->
            Body = <<"<h1>This is a response for other methods</h1>">>
    end,
    {ok, Req3} = cowboy_req:reply(200, [], Body, Req2),
    {ok, Req3, State}.

要获取 POST 请求的内容,您可以使用函数 cowboy_req:body_qs/2 例如。在cowboy 中还有其他处理HTTP 请求主体的函数。检查文档并选择对您方便的方式。

于 2013-12-03T10:27:47.523 回答
0

您可以使用“cowboy_rest”,例如:https ://github.com/extend/cowboy/blob/master/examples/rest_pastebin/src/toppage_handler.erl

它为您提供了一种更结构化的方式来处理请求。

更多信息: http: //ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest/

于 2014-01-03T14:44:01.190 回答
0

你可以使用 cowboy_rest,实现 content_types_accepted/2 回调方法,如下所示:

 content_types_accepted(Req, State) ->
       case cowboy_req:method(Req) of
         {<<"POST">>, _ } ->
           Accepted = {[{<<"application/x-www-form-urlencoded">>, put_file}], Req, State};
         {<<"PUT">>, _ } ->
           Accepted = {[{<<"application/x-www-form-urlencoded">>, post_file}], Req, State}
       end,
 Accepted.

我认为这种方式可以为不同的 HTTP 动词/方法提供单独的处理程序。这也为您提供了更清晰的代码:)

以及各种处理程序:

put_file(Req, State) ->

  {true, Req, State}.

post_file(Req, State) ->

  {true, Req, State}.
于 2017-09-05T13:23:41.350 回答
0

这是一个牛仔中间件,它将方法附加到您的处理程序:

即你handler将成为handler_post

-module(middleware).

-export([execute/2]).

execute(Req0, Env0) ->
    {Method0, Req} = cowboy_req:method(Req0),
    Method1 = string:lowercase(Method0),
    Handler0 = proplists:get_value(handler, Env0),
    Handler = list_to_atom(atom_to_list(Handler0) ++ "_" ++ binary_to_list(Method1)),
    Env = [{handler, Handler} | proplists:delete(handler, Env0)],
    {ok, Req, Env}.
于 2017-09-30T07:59:27.747 回答