直接贴代码,一切尽在不言中

%% cowboy的restful的文档,一定要好好阅读http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest/
%% 几大post提交方式https://imququ.com/post/four-ways-to-post-data-in-http.html
%% curl测试命令curl -l -H "Content-type:application/json" -X POST http://127.0.0.1:8080 -d "aaa=bbb"
-module(restful_handler). -export([init/]).
-export([allowed_methods/]).
-export([content_types_provided/]).
-export([content_types_accepted/]).
-export([delete_completed/]).
-export([delete_resource/]). -export([hello_to_html/]).
-export([form_urlencoded_post/]).
-export([form_data_post/]).
-export([json_post/]). 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("will 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}.

cowboy使用restful的例子的更多相关文章

  1. Spring Boot Hello World (restful接口)例子

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  2. cowboy动态页面的例子

    cowboy的动态页用的是erlydtl,需要先安装erlydtl模板引擎,或者你在dep里面添加 创建工程 rebar-creator create-app testCowboy testCowbo ...

  3. cowboy页面重定向的例子

    创建工程 rebar-creator create-app testCowboy testCowboy_app.erl -module(testCowboy_app). -behaviour(appl ...

  4. JAX-RS 方式的 RESTful Web Service 开发

    JAX-RS 方式的 RESTful Web Service 开发 ——基于 CXF+Spring 的实现 Web Service 目前在风格上有两大类,一个是基于 SOAP 协议,一个是完全遵循 H ...

  5. springMVC学习(11)-json数据交互和RESTful支持

    一.json数据交互: json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据. springMVC进行json交 ...

  6. 11_springmvc之RESTful支持

    一.理解RESTful RESTful架构,就是一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. RESTful(即Representational Sta ...

  7. 利用WCF搭建RESTful--纯代码启动

    最近学习了这几年忽略了的当前几乎所有的开发技术,有深有浅,而服务层最有兴趣的是RESTfull,看的是java的书.因为不熟悉JSP,于是找了本书细细研读了一次. dotnet的实现也相对简单,网上也 ...

  8. 详解REST架构风格

    编辑推荐: 本文来自于segmentfault.com,一起了解REST的内在,认识REST的优势,而不再将它当作是“理所当然” 引言 作为Web开发者,你可能或多或少了解一些REST的知识,甚至已经 ...

  9. spring boot集成mybatis(1)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

随机推荐

  1. Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  2. ehcache 缓存

    一:详细配置步骤 1,添加ehcache.xml文件 将ehcache.xml文件添加到src路径下面.ehcache.xml文件内容如下 <ehcache> <diskStore  ...

  3. AXIOM

    AXIOM是一个实现了延迟构造和拉(pull parsing)解析的轻量级的xml解析器 http://reeboo.iteye.com/blog/317391 http://reeboo.iteye ...

  4. Docker仓库管理

    1.# docker pull registry //下载registry镜像,registry为docker官方提供的一个镜像,我们可以用它来创建本地的docker私有仓库. 2.# docker ...

  5. Codeforces Round #142 (Div. 2)

    A. Dragons 按\(x\)排序. B. T-primes \(x\)是平方数,且根\(\sqrt{x}\)是个质数. C. Shifts 枚举列的位置,对于每行来说,最多只会涉及4个列. D. ...

  6. PHP 页面自动刷新可借助JS来实现,简单示例如下:

    <?php  echo "系统当前时间戳为:"; echo ""; echo time(); //<!--JS 页面自动刷新 --> echo ...

  7. [转]ssh常用用法小结

    ssh常用用法小结 1.连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以上两种方式都可以远程登录到远程主机, ...

  8. CentOS怎样强制卸载PHP以及自定义安装PHP

    很无语,CentOS居然php版本才5.1.6,很多开源的CMS无法安装. 查看php版本命令: #php -v 这个命令是删除不干净的 #yum remove php 因为使用这个命令以后再用 #p ...

  9. java linux book

    calvin1978.blogcn.com/articles/javabookshelf.html

  10. Firefox-常用扩展

    抓包: HttpFox,相比 Firebug 在页面跳转或刷新时依旧保持原有数据 常用User-Agent模拟: User Agent Switcher 更改请求头: ModifyHeaders 更改 ...