cowboy的get和post的例子
官方get和post的代码是有问题的,1.1下运行crash,这里修改了下,贴代码
创建工程
rebar-creator create-app testCowboy
testCowboy_app.erl
-module(testCowboy_app). -behaviour(application). -export([start/2, stop/1]). -define(C_ACCEPTORS, 100). start(_StartType, _StartArgs) ->
application:start(crypto),
application:start(cowlib),
application:start(ranch),
application:start(cowboy), Routes = route_helper:get_routes(),
Dispatch = cowboy_router:compile(Routes),
Port = 8080,
TransOpts = [{port, Port}],
ProtoOpts = [{env, [{dispatch, Dispatch}]}],
cowboy:start_http(http, ?C_ACCEPTORS, TransOpts, ProtoOpts). stop(_State) ->
ok.
route_helper.erl
-module(route_helper). -export([get_routes/0]). get_routes() ->
[
{'_', [
{"/get", get_handler, []},
{"/post", post_handler, []}
]}
].
get_handler.erl
-module(get_handler). -export([init/3]).
-export([handle/2]).
-export([terminate/3]). init(_Transport, Req, []) ->
{ok, Req, undefined}. handle(Req, State) ->
{Method,_} = cowboy_req:method(Req),
{Val,_} = cowboy_req:qs_val(<<"test_get">>,Req),
{ok, Req2} = handle_params(Method, Val, Req),
{ok, Req2, State}. terminate(_Reason, _Req, _State) ->
ok. %% private
handle_params(<<"GET">>, undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
handle_params(<<"GET">>, Val, Req) ->
cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
], Val, Req);
handle_params(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
post_handler.erl
-module(post_handler). -export([init/3]).
-export([handle/2]).
-export([terminate/3]). init(_Transport, Req, []) ->
{ok, Req, undefined}. handle(Req, State) ->
{Method,_} = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req),
{ok, Req2} = handle_params(Method, HasBody, Req),
{ok, Req2, State}. terminate(_Reason, _Req, _State) ->
ok. %% private
handle_params(<<"POST">>, true, Req) ->
{ok, PostVals, Req2} = cowboy_req:body_qs(Req),
Echo = proplists:get_value(<<"echo">>, PostVals),
echo(Echo, Req2);
handle_params(<<"POST">>, false, Req) ->
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
handle_params(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req). echo(undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(Echo, Req) ->
cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
], Echo, Req).
get的测试url
http://127.0.0.1:8080/get?test_get=b
post的测试
http://127.0.0.1:8080/post
字段echo,value随便
cowboy的get和post的例子的更多相关文章
- cowboy的cookie和session的例子
session插件需要下载https://github.com/chvanikoff/cowboy_session 如果session需要分布式存储,可以参考https://github.com/sp ...
- cowboy的例子
大体参考的这里,非常感谢他的例子 开发的时候先下载好cowboy的库,放到~/.erlang里面 code:add_pathz("/Users/mmc/erlang/3rd_libs/cow ...
- cowboy使用restful的例子
直接贴代码,一切尽在不言中 %% cowboy的restful的文档,一定要好好阅读http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest ...
- cowboy动态页面的例子
cowboy的动态页用的是erlydtl,需要先安装erlydtl模板引擎,或者你在dep里面添加 创建工程 rebar-creator create-app testCowboy testCowbo ...
- cowboy页面重定向的例子
创建工程 rebar-creator create-app testCowboy testCowboy_app.erl -module(testCowboy_app). -behaviour(appl ...
- [翻译][erlang]cowboy handler模块的使用
关于Cowboy Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. Handlers,用于处理HTTP请求的程序处理模块. Plain HTTP Handlers ...
- [翻译][erlang]cowboy路由模块使用
Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. 本文官方原文:http://ninenines.eu/docs/en/cowboy/1.0/guide/rout ...
- Erlang cowboy 处理不规范的client
Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...
- Erlang cowboy routing 路由
Erlang cowboy routing 路由 本文译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/ Routing 默认情况下,C ...
随机推荐
- Mac安装MySQLdb遇到的坑
最近项目移植, 再进行virtualenv环境安装的时候遇到mysql-python死活安装失败 首先是这个错误: sh: /usr/local/bin/mysql_config: No such f ...
- Linux中jar包指定端口启动并记录日志
Linux中jar包指定端口启动并记录日志: java -jar -Dserver.port=38080 group-buying-0.0.1-SNAPSHOT.jar >log.log ...
- web前端看IE11的变化
一.User-agent的变化 IE11的User-agent Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko IE10的U ...
- 快速切题 poj 1003 hangover 数学观察 难度:0
Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103896 Accepted: 50542 Descr ...
- nginx的编译,和简单的配置问题
反向代理常见的lvs.haproxy. 缓存服务常见的.squid.vanish.常见的前端缓存.Apache是多进程的web服务器,Nginx是多线程的web服务器. Nginx的特点,对静态能力强 ...
- 通过格式化字符串漏洞绕过canary
1.1 canary内存保护机制 1.1.1 canary工作原理 canary保护机制类似于/GS保护机制,是Linux下gcc编译器的安全保护机制之一,在栈中的结构如下图所示: 在函数 ...
- progressbar进度条组件
Progressbar 进度条组件 通过$.fn.progressbar.fn.defaults重写默认的defaults进度条(progressbar)提供了一种显示长时间操作进度的反馈.进度可被更 ...
- APUE学习笔记——5.5~5.7数据流的打开与读写
1.open #include <stdio.h> FILE *fopen(const char *restrict pathname,const char *restrict type) ...
- APUE学习笔记——3.文件共享与fcntl介绍
基本概念 内核使用3个数据结构描述一个打开的文件:进程表.文件表.V节点表 首先了解3种数据结构的概念 1 进程表 每一个进程有一个进程表.进程表里是一组打开的文件描述符,如标 ...
- AS3帮助手册
http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/index.html