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 ...
随机推荐
- os模块、文件压缩 、匹配文件后缀名:fnmatch glob
一.os模块 os模块:是python是系统交互的模块 import os # 0平台信息的一些操作 python是夸平台的,所以内部兼容了不同的平台 1. os.name # 操作系统 nt是win ...
- python:打包成exe程序
1.需要安装 py2exe 2.示例代码: #exetest.py #创建一个gui界面,只用一个标签和按钮,无功能 from Tkinter import * win = Tk() label = ...
- GitHub:Git的使用
1.下载安装后设置姓名和邮箱地址 $ git config --global user.name "yourGithubName" $ git config --global us ...
- react 入门的好东西 可以做出一个完整的网站
链接 (包含了antd 组件的使用) 安装依赖报错问题 可能需要按顺序安装, 不能cnpm npm 混合安装, 参考这个package.js ...
- Getsystime()与Getlocaltime()函数 相差8个小时
转自 http://xujinzeng.blog.163.com/blog/static/260083420086114747452/ 今天看一个有关时间的例程,发现Getsystime()与Getl ...
- Android 你可能忽略的提高敲代码效率的方式
Android 你可能忽略的提高敲代码效率的方式
- loadrunner11 中文破解版安装教程
loadrunner11的安装:http://pan.baidu.com/share/link?shareid=316642707&uk=1395568298 汉化包(下载之后有可能是ISO格 ...
- [Linux] jq:命令行JSON处理工具
jq命令帮助我们很方便地在终端查看和处理json文件 jq命令的帮助信息: abby@abby:bgs$ jq -h jq - commandline JSON processor [version ...
- 使用gmock白盒测试
提起白盒测试,很多程序员可能觉得就是个书上的概念,很多人写完代码根本没有具体的测试方案,自己觉得可行就提交了,其实这是个很危险的事情,毕竟出了bug,最后要加班的人还是你 ,因此做好白盒测试,100% ...
- 使用TR1的智能指针
作为C++程序员,在没有智能指针,手动管理内存的蛮荒岁月里,可以说是暗无天日,痛苦异常.直到上帝说,还是要有光,于是智能指针进了标准.C++码农的日子总算好起来了. 虽然一直鄙视着没有显式指针的语言, ...