如何使用处理程序中的erlang中的content_type_accepted方法来发布Json数据

时间:2015-01-22 16:57:54

标签: erlang cowboy

这是json数据

     { 
         "title": "The Title", 
         "content": "The Content" 
     }  

    curl -vX POST http://localhost:10003/sample -H "Content-Type:application/json" \ -d '{ "title": "The Title", "content": "The Content" }'
    -export([content_types_accepted/2]).
    allowed_methods(Req, State) ->  
        {[<<"GET">>, <<"POST">>], Req, State}.  

    content_types_accepted(Req, State) ->
    {[{{<<"application">>, <<"json">>, []}, welcome}], Req, State}.
    welcome(Req, State) ->
        Req_method = cowboy_req:method(Req),
        io:format("Req_method is ~p ~n", [Req_method]),
        Req_Body = cowboy_req:body(Req),
        io:format("Body is ~p ~n", [Req_Body]),
        Body = <<"<h1>This is a response for other methods</h1>">>,
        io:format("Body is  ~p ~n",[Body]),
        {Body, Req, State}.

我看到方法和正文,但试图抓住无法执行此操作的json数据。

3 个答案:

答案 0 :(得分:2)

我一直在寻找http-Post-method,使用content_type_accepted方法传递Json数据。以下代码对我有用:)

    -export([init/3]).

    -export([welcome/2, terminate/3, allowed_methods/2]).
    -export([content_types_accepted/2]).
    init(_Transport, _Req, []) ->
        {upgrade, protocol, cowboy_rest}.
    allowed_methods(Req, State) ->  
        {[<<"POST">>], Req, State}.  
    content_types_accepted(Req, State) ->
    {[{<<"application/json">>, welcome}], Req, State}.
    terminate(_Reason, _Req, _State) ->
        ok.
    welcome(Req, State) ->
        {ok, ReqBody, Req2} = cowboy_req:body(Req),
        Req_Body_decoded = jsx:decode(ReqBody),
        [{<<"title">>,Title},{<<"content">>,Content}] = Req_Body_decoded,
        Title1 = binary_to_list(Title),
        Content1 = binary_to_list(Content),
        io:format("Title1 is ~p ~n ", [Title1]),
        io:format("Content1 is ~p ~n", [Content1]),
        io:format("Title is ~p ~n", [Title]),
        io:format("Content is ~p ~n", [Content]),
        lager:log(info, [], "Request Body", [Req_Body_decoded]),
        Res1 = cowboy_req:set_resp_body(ReqBody, Req2),
        Res2 = cowboy_req:delete_resp_header(<<"content-type">>, Res1),
        Res3 = cowboy_req:set_resp_header(<<"content-type">>, <<"application/json">>, Res2),
        {true, Res3, State}.

答案 1 :(得分:1)

在Erlang中进行HTTP POST非常简单。摘自this other StackOverflow answer,这是一个简单的例子:

ssl:start(),
application:start(inets),
PostBody = "{ \"title\": \"The Title\", \"content\": \"The Content\" }",
Url = "http://some.url/endpoint",
httpc:request(post, 
    {Url, [], 
    "application/x-www-form-urlencoded",
    PostBody
    }, [], []).

希望这有帮助!

这些资源似乎也会有所帮助:

答案 2 :(得分:0)

要捕获json值,您可以使用jiffy https://github.com/davisp/jiffy

使用示例:

{JsonDecode} = jiffy:decode(ReqBody),
error_logger:info_msg("JSON Decoded by jiffy ", JsonDecode),

UserId = proplists:get_value(<<"userid">>, JsonDecode),
Amount = proplists:get_value(<<"amount">>, JsonDecode),
Rate = proplists:get_value(<<"rate">>, JsonDecode),
Duration = proplists:get_value(<<"duration">>, JsonDecode),