1

这是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}.

我看到了 Method 和 Body,但试图捕获无法这样做的 json 数据。

4

3 回答 3

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}.
于 2015-01-23T14:36:55.207 回答
1

在 Erlang 中创建 HTTP POST 本身非常简单。取自另一个 StackOverflow 答案,这是一个简单的示例:

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
    }, [], []).

希望这可以帮助!

这些资源似乎也很有帮助:

于 2015-01-22T18:58:17.290 回答
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),
于 2017-09-05T13:36:01.807 回答