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

如何在处理程序中使用erlang中的content_type_accepted方法发布Json数据,erlang,cowboy,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([c

下面是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}.
{
“头衔”:“头衔”,
“内容”:“内容”
}  
curl-vX桩http://localhost:10003/sample -H“内容类型:应用程序/json”\-d'{“标题”:“标题”,“内容”:“内容”}”
-导出([content\u types\u accepted/2])。
允许的方法(请求、状态)->
{[,],请求,状态}。
内容类型接受(请求、状态)->
{[{,[]},欢迎}],请求,状态}。
欢迎(请求、状态)->
要求方法=牛仔要求:方法(要求),
io:格式(“Req_方法为~p~n”,[Req_方法],
要求车身=牛仔要求:车身(要求),
io:格式(“正文为~p~n”,[Req_Body]),
正文=>,,
io:format(“Body是~p~n”,[Body]),
{Body,Req,State}。

我看到了方法和正文,但试图捕获json数据却无法做到这一点。

在Erlang中编写HTTP POST本身非常简单。以下是一个简单的例子:

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
    }, [], []).
希望这有帮助

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


我在寻找http Post方法,使用content\u type\u接受的方法传递Json数据。以下是对我有效的代码:)

-导出([init/3])。
-导出([欢迎/2,终止/3,允许的方法/2])。
-导出([content\u types\u accepted/2])。
初始化(_传输,_请求,[])->
{升级,协议,牛仔休息}。
允许的方法(请求、状态)->
{[],请求,状态}。
内容类型接受(请求、状态)->
{[{,欢迎}],请求,状态}。
终止(原因、要求、状态)->
好啊
欢迎(请求、状态)->
{ok,ReqBody,Req2}=cowboy_req:body(req),
Req_Body_decoded=jsx:decode(ReqBody),
[{,Title},{,Content}]=Req_Body_解码,
Title1=二进制列表(标题),
Content1=二进制到二进制列表(Content),
io:格式(“标题1为~p~n”,[Title1]),
io:格式(“Content1是~p~n,[Content1]),
io:格式(“标题为~p~n”,[Title]),
io:格式(“内容为~p~n,[Content]),
lager:log(信息,[],“请求主体”,[Req\u Body\u decoded]),
Res1=牛仔要求:设置相应的车身(要求车身,要求2),
Res2=牛仔要求:删除相应标题(,Res1),
Res3=牛仔要求:设置响应头(,Res2),
{true,Res3,State}。

要捕获json值,可以使用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),
{JsonDecode}=jiffy:decode(ReqBody),
错误日志:info msg(“jiffy解码的JSON”,JsonDecode),
UserId=proplist:get_值(,JsonDecode),
Amount=proplist:get_值(,JsonDecode),
Rate=proplist:get_值(,JsonDecode),
Duration=proplist:get_值(,JsonDecode),

您能提供更多详细信息吗?也许您已经编写了一些代码?或者解释一下你尝试了什么,为什么失败了?我们将需要更多的信息,以提供良好的答案。可能的副本,我很抱歉,但这不是我要寻找的。我对Erlang非常陌生,我的要求是使用content_types_accepted(用于POST)发布HTTP方法,就像我对content_types_provide(用于get)所做的那样。我花了一整天的时间在IRB和其他博客/论坛上搜索,没有结果,所以最后我在这里发布了问题。正如你们所能看到的,我几乎通过内容类型接受的方法完成了,它只是停留在捕捉json值上。
{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),