1

Here is the json data

     { 
         "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}.

I see Method and Body but trying to catch json data unable to do such.

KJ_kaka
  • 241
  • 3
  • 10
  • 1
    Can you provide more details? Perhaps some of the code you have already written? Or perhaps explain what you have tried and why it failed? We will need more information in order to provide good answers. – Stratus3D Jan 22 '15 at 18:50
  • possible duplicate of [Simple example using Erlang for https post](http://stackoverflow.com/questions/19103694/simple-example-using-erlang-for-https-post) – Stratus3D Jan 22 '15 at 18:57
  • I am very sorry But this is not I am looking for. I am very new to Erlang and my requirement is to POST HTTP method using content_types_accepted(for post) as I am done with content_types_provide(for get). I spent whole day searching on IRB and other Blogs/forum no result so finally I posted question here. – KJ_kaka Jan 23 '15 at 03:21
  • As you can I am almost done through **content_type_accepted** method, It's just stuck with catching json values. – KJ_kaka Jan 23 '15 at 03:29

3 Answers3

2

I was looking for http-Post-method, passing Json data using content_type_accepted method. Here is the code which worked for me :)

    -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}.
KJ_kaka
  • 241
  • 3
  • 10
1

Making an HTTP POST in Erlang itself is very simple. Taken from this other StackOverflow answer, here is a simple Example:

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

Hope this helps!

These resources also seem like they would be helpful:

Community
  • 1
  • 1
Stratus3D
  • 4,648
  • 4
  • 35
  • 67
0

To catch the json values you can use jiffy https://github.com/davisp/jiffy

Example usage :

{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),
Oladipo Olasemo
  • 2,010
  • 24
  • 31