官方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的例子的更多相关文章

  1. cowboy的cookie和session的例子

    session插件需要下载https://github.com/chvanikoff/cowboy_session 如果session需要分布式存储,可以参考https://github.com/sp ...

  2. cowboy的例子

    大体参考的这里,非常感谢他的例子 开发的时候先下载好cowboy的库,放到~/.erlang里面 code:add_pathz("/Users/mmc/erlang/3rd_libs/cow ...

  3. cowboy使用restful的例子

    直接贴代码,一切尽在不言中 %% cowboy的restful的文档,一定要好好阅读http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest ...

  4. cowboy动态页面的例子

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

  5. cowboy页面重定向的例子

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

  6. [翻译][erlang]cowboy handler模块的使用

    关于Cowboy Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. Handlers,用于处理HTTP请求的程序处理模块. Plain HTTP Handlers ...

  7. [翻译][erlang]cowboy路由模块使用

    Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. 本文官方原文:http://ninenines.eu/docs/en/cowboy/1.0/guide/rout ...

  8. Erlang cowboy 处理不规范的client

    Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...

  9. Erlang cowboy routing 路由

    Erlang cowboy routing 路由 本文译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/ Routing 默认情况下,C ...

随机推荐

  1. SHOW INNODB STATUS 探秘

    [InnoDB系列] -- SHOW INNODB STATUS 探秘 SHOW INNODB STATUS 探秘 转载:http://imysql.com/2008_05_22_walk_throu ...

  2. 基于vue-cli,测试非父子传值时,碰到 keep-alive的神奇

    非父子传值测试 一直都很好奇非父子传值到底如何,结果入坑许久才爬出来,才知道在脚手架里测试就是坑. 问题: 测试非父子传值时,由于组件之间是通过路由进行跳转,值传过去又被刷掉 思路: 因为路由跳转,相 ...

  3. Sum All Numbers in a Range

    我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduc ...

  4. MYSQL-实现分组排序 对比 ORACLE 和SQLserver用 row_number() over(partition by ) 分组排序功能

    以下是个人笔记: 本文是为了理解 row_number() over(partition by )  和实现各种数据库的分组排序功能 select ROW_NUMBER()over( partitio ...

  5. 在 Bash on Ubuntu 上安装Nginx

    前言 Win10 上的 Bash on Ubuntu 是个很好用的玩具,让windows开发环境下的人能无缝操练Linux,但是涉及到网络部分还是有很多要该进的地方,比如Nginx的安装就遇到了问题. ...

  6. HashMap resize方法的理解(一)

    对于oldTable中存储的为15.7.4.5.8.1,长度为8的一个数组中,存储位置如下 0 1 2 3 4 5 6 7 8 1 4 5 15 7 当扩容到一倍后,对于新的位置的选择通过e.hash ...

  7. L154

    Several possessions of the late physicist's Stephen Hawking will be included in an upcoming auction ...

  8. New Concept English three(13)

    1原文打字 32w/m 错词27个 After her husband had gone to work, Mrs Richards sent her children to school and w ...

  9. Makefile特殊标签

    http://www.gnu.org/software/make/manual/html_node/Special-Targets.html

  10. GPU编程自学5 —— 线程协作

    深度学习的兴起,使得多线程以及GPU编程逐渐成为算法工程师无法规避的问题.这里主要记录自己的GPU自学历程. 目录 <GPU编程自学1 -- 引言> <GPU编程自学2 -- CUD ...