cowboy使用restful的例子

直接贴代码,一切尽在不言中

%% cowboy的restful的文档,一定要好好阅读http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest/
%% 几大post提交方式https://imququ.com/post/four-ways-to-post-data-in-http.html
%% curl测试命令curl -l -H "Content-type:application/json" -X POST http://127.0.0.1:8080 -d "aaa=bbb"
-module(restful_handler).

-export([init/2]).
-export([allowed_methods/2]).
-export([content_types_provided/2]).
-export([content_types_accepted/2]).
-export([delete_completed/2]).
-export([delete_resource/2]).

-export([hello_to_html/2]).
-export([form_urlencoded_post/2]).
-export([form_data_post/2]).
-export([json_post/2]).

init(Req, Opts) ->
  {cowboy_rest, Req, Opts}.

allowed_methods(Req, State) ->
  Methods = [
    <<"HEAD">>,
    <<"GET">>,
    <<"POST">>,
    <<"PATCH">>,
    <<"DELETE">>,
    <<"OPTIONS">>
  ],
  {Methods, Req, State}.

content_types_provided(Req, State) ->
  Handlers = [
    {<<"text/html">>, hello_to_html},

    {<<"application/x-www-form-urlencoded">>, form_urlencoded_post},
    {<<"multipart/form-data">>, form_data_post},
    {<<"application/json">>, json_post}
  ],
  {Handlers, Req, State}.

content_types_accepted(Req, State) ->
  Handlers = [
    {<<"application/x-www-form-urlencoded">>, form_urlencoded_post},
    {<<"multipart/form-data">>, form_data_post},
    {<<"application/json">>, json_post}
  ],
  {Handlers, Req, State}.

delete_completed(Req, State) ->
  io:format("will delete resource~n"),
  {true, Req, State}.

delete_resource(Req, State) ->
  io:format("delete resource finish~n"),
  {true, Req, State}.

hello_to_html(Req, State) ->
  Body = <<"html">>,
  {Body, Req, State}.

form_urlencoded_post(Req, State) ->
  {ok, PostVals, _Req2} = cowboy_req:body_qs(Req),
  PostVal1 = proplists:get_value(<<"aaa">>, PostVals),
  PostVal2 = proplists:get_value(<<"bbb">>, PostVals),
  io:format("form_urlencoded_post~p~p~n",[PostVal1,PostVal2]),

  Body = string:concat(
    erlang:bitstring_to_list(PostVal1),
    erlang:bitstring_to_list(PostVal2)
  ),
  NewReq = cowboy_req:set_resp_body(erlang:list_to_bitstring(Body),Req),
  {true, NewReq, State}.

form_data_post(Req, State) ->
  {ok, PostVals, _Req2} = cowboy_req:body_qs(Req),
  PostVal = proplists:get_value(<<"aaa">>, PostVals),
  io:format("form_data_post~p~n",[PostVal]),
  NewReq = cowboy_req:set_resp_body(PostVal,Req),
  {true, NewReq, State}.

json_post(Req, State) ->
  {ok, PostVals, _Req2} = cowboy_req:body_qs(Req),
  PostVal = proplists:get_value(<<"aaa">>, PostVals),
  io:format("json_post~p~n",[PostVal]),
  NewReq = cowboy_req:set_resp_body(PostVal,Req),
  {true, NewReq, State}.
时间: 2024-07-30 13:46:40

cowboy使用restful的例子的相关文章

Spring Boot Hello World (restful接口)例子

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge

简单的Restful API例子(Golang)

RESTful API 这玩意不用多说了,用Go做了个很简单的例子: 服务端在被调用时返回JSON, 客户端解析得到相关JSON信息. 服务端源码: package main //简单的JSON Restful API演示(服务端) //author: Xiong Chuan Liang //date: 2015-2-28 import ( "encoding/json" "fmt" "net/http" "time" ) t

JAX-RS 方式的 RESTful Web Service 开发

JAX-RS 方式的 RESTful Web Service 开发 ——基于 CXF+Spring 的实现 Web Service 目前在风格上有两大类,一个是基于 SOAP 协议,一个是完全遵循 HTTP 协议规范的 RESTful 风格. SOAP 方式的 web service 已经很成熟了,应用也很广,已经成为 Web Service 的工业标准.不过 RESTful Web Service 现在势头越来越猛,特别是其灵活性以及与 Ajax 的完美结合,让人爱不释手,很有必要了解一下.

详解REST架构风格

编辑推荐: 本文来自于segmentfault.com,一起了解REST的内在,认识REST的优势,而不再将它当作是“理所当然” 引言 作为Web开发者,你可能或多或少了解一些REST的知识,甚至已经非常习惯于它,以至于在正式地学习REST的时候,你可能心里会想:“本来就是这样做的啊,不然还能怎么做呢?” 确实是这样,REST已经成为Web世界的一种内在架构原则.这主要是因为REST的产生确实与HTTP有着密不可分的联系.REST的提出者Roy Fielding在Web界是一位举足轻重的人物,他

Java: 未来已来

前言:在10月22的 Oracle Codeone大会上,Java平台的首席架构师 Mark Reinhold 做了The Future of Java is Today的演讲, 回顾了最近Java的几个版本的新的功能,Java的每年两次的发布周期, 澄清了关于发布流程和Java版本的几个误区,最后花了很大的篇幅介绍了未来Java几个令人非常期待的几个孵化中项目,可以为Java带来更好的生产力.性能和可扩展性.我整理了这四个项目相关的知识,你可以提前了解到Java未来的这些酷炫的特性. Java

spring boot 开发环境搭建(Eclipse)

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge

spring boot redis 缓存(cache)集成

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge

spring boot 环境配置(profile)切换

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge

Spring Boot 介绍

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge