Examples of cowboy using restful

Source: Internet
Author: User

Direct code, everything is not in the words

Percent of cowboy restful documents, be sure to read HTTP well://ninenines.eu/docs/en/cowboy/head/manual/cowboy_rest/Percent of a few large post-submission methods https://imququ.com/post/four-ways-to-post-data-in-http.htmlPercent Curl Test command curl-l-H"Content-type:application/json"-X POST http://127.0.0.1:8080-d "AAA=BBB"-module (restful_handler).-export ([init/2]).-export ([allowed_methods/2]).-export ([content_types_provided/2]).-export ([content_types_accepted/2]).-export ([delete_completed/2]).-export ([delete_resource/2]).-export ([hello_to_html/2]).-export ([form_urlencoded_post/2]).-export ([form_data_post/2]).-export ([json_post/2]). Init (Req, Opts)-{cowboy_rest, Req, Opts}.allowed_methods (Req, State)-Methods= [    <<"HEAD">>,    <<"GET">>,    <<"POST">>,    <<"PATCH">>,    <<"DELETE">>,    <<"OPTIONS">>], {Methods, Req, state}.content_types_provided (Req, State)-handlers= [    {<<"text/html">>, hello_to_html}, {<<"application/x-www-form-urlencoded">>, Form_urlencoded_post}, {<<"Multipart/form-data">>, Form_data_post}, {<<"Application/json">>, Json_post}], {handlers, Req, state}.content_types_accepted (Req, State)-handlers= [    {<<"application/x-www-form-urlencoded">>, Form_urlencoded_post}, {<<"Multipart/form-data">>, Form_data_post}, {<<"Application/json">>, Json_post}], {handlers, Req, state}.delete_completed (Req, State)-Io:format ("Would delete resource~n"),  {true, Req, State}.delete_resource (Req, State)-Io:format ("Delete Resource Finish~n"),  {true, Req, state}.hello_to_html (Req, State)-Body= <<"HTML">>, {Body, Req, State}.form_urlencoded_post (Req, State)-{OK, postvals, _req2}=Cowboy_req:body_qs (req), PostVal1= Proplists:get_value (<<"AAA">>, Postvals), PostVal2= Proplists:get_value (<<"BBB">>, Postvals), Io:format ("Form_urlencoded_post~p~p~n", [Postval1,postval2]), Body=string: Concat (Erlang:bitstring_to_list (POSTVAL1), Erlang:bitstring_to_list (PostVal2)), Newreq=Cowboy_req:set_resp_body (erlang:list_to_bitstring (body), req), {true, Newreq, State}.form_data_post (Req, State)-{OK, postvals, _req2}=Cowboy_req:body_qs (req), Postval= Proplists:get_value (<<"AAA">>, Postvals), Io:format ("Form_data_post~p~n", [Postval]), Newreq=Cowboy_req:set_resp_body (postval,req), {true, Newreq, State}.json_post (Req, State)-{OK, postvals, _req2}=Cowboy_req:body_qs (req), Postval= Proplists:get_value (<<"AAA">>, Postvals), Io:format ("Json_post~p~n", [Postval]), Newreq=Cowboy_req:set_resp_body (postval,req), {true, Newreq, state}.

Examples of cowboy using restful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.