设为首页 加入收藏

TOP

phoenix 开发API系列(二)phoenix 各类 api 实现方式(一)
2017-10-09 13:27:26 】 浏览:952
Tags:phoenix 开发 API 系列 各类 api 实现 方式

概述

上一篇已经提到如何安装以及利用 phoenix framework 来开发一个简单的 api。 接着上次的工程,下面演示如何通过 phoenix framework 来构建各种类型的 api 来应对前端的各类请求。

下面使用的工程的完整代码已经公开在: http://git.oschina.net/wangyubin/phoenix-api

各类 api 的实现示例

restful url 的参数

introduce by code:

  • controller 中相关代码:

    @doc "/api/param/:name"
    def rest_param1(conn, %{"name" => name}) do
      json conn, %{
        "result": "success",
        "message": "your name is " <> name,
      }
    end
    
    @doc "/api/param/:name/:age"
    def rest_param2(conn, %{"name" => name, "age" => age}) do
      json conn, %{
        "result": "success",
        "message": "your name is " <> name <> " and age is " <> age,
      }
    end
  • router 相关代码: (router.ex)
    get "/param/:name", ApiParamController, :rest_param1
    get "/param/:name/:age", ApiParamController, :rest_param2
  • 启动 phoenix 开发服务器,就可以在浏览器中访问对应的 URL
    mix phoenix.server

在 浏览器 中访问 http://localhost:4000/api/param/wanghttp://localhost:4000/api/param/wang/33 可以看到返回的 json。

GET 请求中的参数

introduce by code: api的参数的上面的示例一样

  • controller 中相关代码:(api_param_controller.ex)
    @doc "/api/param?name=xxx&age=yyy"
    def rest_param3(conn, params) do
      if Map.has_key?(params, "age") do
        json conn, %{
          "result": "success from rest_param3",
          "message": "your name is " <> params["name"] <> " and age is " <> params["age"],
        }
      else
        json conn, %{
          "result": "success from rest_param3",
          "message": "your name is " <> params["name"],
        }
      end
    end
  • router 相关代码: (router.ex)
    get "/param", ApiParamController, :rest_param3
  • 启动 phoenix 开发服务器,就可以在浏览器中访问对应的 URL
    mix phoenix.server

在 浏览器 中访问 http://localhost:4000/api/param?name=wang&age=33http://localhost:4000/api/param?name=wang 可以看到返回的 json。

POST 请求中的参数

introduce by code: api的参数的上面的示例一样

  • controller 中相关代码:(api_param_controller.ex)
    @doc "/api/param"
    def post_param(conn, params) do
      if Map.has_key?(params, "age") do
        json conn, %{
          "result": "success from post_param",
          "message": "your name is " <> params["name"] <> " and age is " <> params["age"],
        }
      else
        json conn, %{
          "result": "success from post_param",
          "message": "your name is " <> params["name"],
        }
      end
    end
  • router 相关代码: (router.ex)
    post "/param", ApiParamController, :post_param
  • 启动 phoenix 开发服务器,就可以在浏览器中访问对应的 URL
    mix phoenix.server

测试api 可以使用 curl 命令:

    curl -X POST -H "Cache-Control: no-cache" -F "name=wyb" "http://localhost:4000/api/param"
    curl -X POST -H "Cache-Control: no-cache" -F "name=wyb" -F "age=33" "http://localhost:4000/api/param"

json 格式参数

introduce by code: api的参数的上面的示例一样

  • controller 中相关代码:(api_param_controller.ex)
    @doc "/api/json-param"
    def json_param(conn, params) do
      if Map.has_key?(params, "age") do
        json conn, %{
          "result": "success from json_par
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇云巴:基于 MQTT 协议的实时通信.. 下一篇酒罢问君三语

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目