首页 技术 正文
技术 2022年11月10日
0 收藏 691 点赞 5,110 浏览 2207 个字

官方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随便
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297