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

%% 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. poj 1190 生日蛋糕

    中文题 题目分析 搜索题,非常好的剪枝 由于深度一定(m),所以使用深度优先搜索,自上而下的设定蛋糕序号,最顶层的为第1层,……,最底层的蛋糕为第m层,很明显满足题目条件的前i层的(从顶层(也就是编号 ...

  2. 经典 socket通讯 -- 已验证

    [tcp通信] 来源:不详: client: package com.defonds.socket.begin; import java.io.BufferedReader; import java. ...

  3. easyUI分页显示

    struts2: http://www.cnblogs.com/huozhicheng/archive/2011/09/27/2193605.html springMVC http://blog.cs ...

  4. 工作中遇到的问题--缓存配置(使用@Configuration装配 @Bean的方式注入)

    @EnableCaching@Configurationpublic class MFGCachingConfiguration { @Autowired private MFGSettings mf ...

  5. URAL 1085 Meeting(最短路)

    Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...

  6. URAL 2034 Caravans(变态最短路)

    Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...

  7. 《笨办法学Python》

    习题一 第一个程序 print "Hello World!" print "Hello Evilxr" print "I like typing th ...

  8. iPad 2升级iOS 9的过程记录

    有一台老旧的iPad2,iOS版本还是5.1.1,现在好多软件都无法安装了. 决定升级到最新的操作系统,中间的过程,遇到的问题和解决办法如下: 据说升级到iOS 9以后就不好越狱了,不过我也就是用用一 ...

  9. [hdu 4416]Good Article Good sentence

    最近几天一直在做有关后缀自动机的题目 感觉似乎对后缀自动机越来越了解了呢!喵~ 这题还是让我受益颇多的,首先搞一个后缀自动机是妥妥的了 可是搞完之后呢? 我们来观察 step 这个变量,每个节点的 s ...

  10. 安装了iis之后,打开默认网站http://localhost/要求输入用户名和密码解决办法

        开始-运行gpedit.msc回车.     计算机配置--管理模板-windows 组件-Internet Exporer-Internet控制面板-安全页-Internet区域:双击登陆选 ...