• 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}.
  • 相关阅读:
    抓取猫眼电影top100的正则、bs4、pyquery、xpath实现方法
    Win实用好用软件清单推荐
    Manjaro安装配置美化字体模糊发虚解决记录
    爬取杭电oj所有题目
    Python爬取微博热搜以及链接
    20191225_Python构造函数知识以及相关注意事项
    java_细节_windows7下记事本保存为utf-8格式的问题
    基础_划分子网
    爬虫_爬取有道每日一句
    算法_基础_伪代码定义以及遵循的规则
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/4869831.html
Copyright © 2020-2023  润新知