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 ...
随机推荐
- Sum All Numbers in a Range
我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduc ...
- SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1
5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...
- elasticsearch关于索引切分的实现
[背景信息] ES一直以来对于已经创建好的索引的分片是不可以进行分割的,简单的说,当你创建了一个索引,并指定了number_of_shards为2,当随着数据量的不断增大,是无法将索引的shard扩充 ...
- LINUX系统下的磁盘共享
在一台主机上实现磁盘的共享:在另一台主机中进行使用.具体操作如下: 1)在服务器端(server) 1.安装软件和磁盘的划分 yum install targetcli.noarch -y ...
- 解如何利用 XML 和 JavaScript Object Notation 在 Ajax 客户端和 Java 服务器之间传输数据(代码)(Oracle)。
---------------------------------ajaxUtil----------------------------------------------------------- ...
- JDBC的步骤
使用jdbc步骤 a.导入数据库厂商提供的驱动程序(导入jar包) b.加载驱动程序 Class.forName("驱动程序类") c.获得连接 Connection conn=D ...
- ORA-06512: at "SYS.XMLTYPE" 问题记录
执行SQL报错: SQL> SELECT COUNT(cl.enable_flg) FROM cont_ledger cl INNER JOIN project_project pp ON cl ...
- Android 开发 Tip 17 -- 为什么getBackground().setAlpha(); 会影响别的控件?
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/75670018 http://www.jb51.net/article/110035.h ...
- key相同合并Map
public class Demo11 { public static void main(String[] args) { ConcurrentHashMap<Integer, Map< ...
- 【Python】下载图片
import requests import bs4 import urllib.request import urllib import os hdr = {'User-Agent': 'Mozil ...