phoenix 开发API系列(二)phoenix 各类 api 实现方式。phoenix 开发API系列(二)phoenix 各类 api 实现方式。

by admin on 2018年10月17日

概述

直达同样篇都关系如何设置及采用 phoenix framework 来出一个大概的 api。
接着上次的工程,下面演示如何通过 phoenix framework 来构建各种类型的 api
来应本着前者的个请求。

下面用的工程的总体代码已经公开于:
http://git.oschina.net/wangyubin/phoenix-api

概述

达到一样篇已关系如何设置和下 phoenix framework 来支付一个粗略的 api。
接着上次的工程,下面演示如何通过 phoenix framework 来构建各种类型的 api
来应本着前者的个请求。

下面用的工的共同体代码已经公开于:
http://git.oschina.net/wangyubin/phoenix-api

位 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/wang 和
http://localhost:4000/api/param/wang/33 可以看到返回的 json。

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/wang 和
http://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=33 和
http://localhost:4000/api/param?name=wang 可以视返回的 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=33 和
http://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"

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_param",
          "message": "your name is " <> params["name"] <> " and age is " <> to_string(params["age"]),
        }
      else
        json conn, %{
          "result": "success from json_param",
          "message": "your name is " <> params["name"],
        }
      end
    end
    
  • router 相关代码: (router.ex)

    post "/json-param", ApiParamController, :json_param
    
  • 启航 phoenix 开发服务器,就可以以浏览器被做客对应的 URL

    mix phoenix.server
    

测试api 可以使 curl 命令:

    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
        "name": "wyb"
    }' "http://localhost:4000/api/json-param"

    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
        "name": "wyb",
        "age": 33
    }' "http://localhost:4000/api/json-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_param",
          "message": "your name is " <> params["name"] <> " and age is " <> to_string(params["age"]),
        }
      else
        json conn, %{
          "result": "success from json_param",
          "message": "your name is " <> params["name"],
        }
      end
    end
    
  • router 相关代码: (router.ex)

    post "/json-param", ApiParamController, :json_param
    
  • 启动 phoenix 开发服务器,就可在浏览器被走访对应之 URL

    mix phoenix.server
    

测试api 可以动用 curl 命令:

    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
        "name": "wyb"
    }' "http://localhost:4000/api/json-param"

    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
        "name": "wyb",
        "age": 33
    }' "http://localhost:4000/api/json-param"

下载 文件

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

  • controller 中并行关代码:(api_param_controller.ex)

    @doc "/api/file-param"
    def file_param(conn, params) do
      filepath = "/tmp/downloadfile.txt"
      if Map.has_key?(params, "age") do
        File.write(filepath, "your name is " <> params["name"] <> " and age is " <> to_string(params["age"]))
      else
        File.write(filepath, "your name is " <> params["name"])
      end
    
      conn |> send_file(200, filepath)
    end
    
  • router 相关代码: (router.ex)

    get "/file-param", ApiParamController, :file_param
    
  • 起步 phoenix 开发服务器,就得在浏览器被走访对应之 URL

    mix phoenix.server
    

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

下载 文件

introduce by code: api的参数的点的示范一样

  • controller 中互关代码:(api_param_controller.ex)

    @doc "/api/file-param"
    def file_param(conn, params) do
      filepath = "/tmp/downloadfile.txt"
      if Map.has_key?(params, "age") do
        File.write(filepath, "your name is " <> params["name"] <> " and age is " <> to_string(params["age"]))
      else
        File.write(filepath, "your name is " <> params["name"])
      end
    
      conn |> send_file(200, filepath)
    end
    
  • router 相关代码: (router.ex)

    get "/file-param", ApiParamController, :file_param
    
  • 启航 phoenix 开发服务器,就可以在浏览器中访问对应之 URL

    mix phoenix.server
    

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

上传 文件

introduce by code: api的参数的端的示范一样

  • controller 中互相关代码:(api_param_controller.ex)

    @doc "/api/file-param"
    def upload_param(conn, params) do
    
      file = params["file"]
      File.cp(file.path, "/tmp/upload.file")
    
      json conn, %{
        "result": "success from file_param",
        "message": "your name is " <> params["name"] <> " and age is " <> to_string(params["age"])
        <> " and the filename which you upload is " <> file.filename,
      }
    end
    
  • router 相关代码: (router.ex)

    post "/file-param", ApiParamController, :upload_param
    
  • 起先 phoenix 开发服务器,就好在浏览器中做客对应之 URL

    mix phoenix.server
    

测试api 可以用 curl 命令: 命令中的 file 要替换成你的实际上文件路径

    curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" \
        -F "name=wyb" -F "age=33" -F "file=@/tmp/test.jpg" "http://localhost:4000/api/file-param"

上传 文件

introduce by code: api的参数的方的演示一样

  • controller 中相互关代码:(api_param_controller.ex)

    @doc "/api/file-param"
    def upload_param(conn, params) do
    
      file = params["file"]
      File.cp(file.path, "/tmp/upload.file")
    
      json conn, %{
        "result": "success from file_param",
        "message": "your name is " <> params["name"] <> " and age is " <> to_string(params["age"])
        <> " and the filename which you upload is " <> file.filename,
      }
    end
    
  • router 相关代码: (router.ex)

    post "/file-param", ApiParamController, :upload_param
    
  • 启动 phoenix 开发服务器,就可以浏览器中走访对应的 URL

    mix phoenix.server
    

测试api 可以行使 curl 命令: 命令中的 file 要替换成你的实在文件路径

    curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" \
        -F "name=wyb" -F "age=33" -F "file=@/tmp/test.jpg" "http://localhost:4000/api/file-param"

总结

得看岀,phoenix framework 的 Plug 提供了长的功力,所以编写 api
非常有利。 掌握了上面的以身作则,基本就可满足构建web服务经常大部分底 api
的勾法了。

来源:http://blog.iotalabs.io/

总结

可看岀,phoenix framework 的 Plug 提供了增长的意义,所以编写 api
非常有利于。 掌握了地方的示范,基本就是足以满足构建web服务时大部分的 api
的刻画法了。

来源:http://blog.iotalabs.io/

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图