【问题标题】:What is the equivalent of 'head :ok' from Rails in Phoenix?凤凰城 Rails 中的“head :ok”相当于什么?
【发布时间】:2016-09-16 00:02:17
【问题描述】:

我想返回一个没有内容(仅是标题)的响应,例如 this one

def show
  head :ok
end

【问题讨论】:

    标签: ruby-on-rails elixir phoenix-framework


    【解决方案1】:

    您可以将Plug.Conn.send_resp/3 与空正文一起使用:

    # 200 OK
    send_resp(conn, 200, "")
    send_resp(conn, :ok, "") # same as above
    # 401 Unauthorized
    send_resp(conn, 401, "")
    send_resp(conn, :unauthorized, "") # same as above
    

    send_resp 可以将状态(第二个参数)作为整数或此处提到的受支持原子之一:https://hexdocs.pm/plug/Plug.Conn.Status.html#code/1

    【讨论】:

      【解决方案2】:

      @dogbert 的回答很到位。此外,您可以阅读官方phoenix guide 的相关文档。相关资料-http://www.phoenixframework.org/docs/controllers#section-sending-responses-directly

      ...让我们 假设我们要发送状态为“201”且没有正文的响应 任何。我们可以使用 send_resp/3 函数轻松做到这一点。

      def index(conn, _params) do
        conn
        |> send_resp(201, "")
      end
      

      【讨论】:

        猜你喜欢
        • 2015-09-06
        • 2016-05-03
        • 1970-01-01
        • 2017-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        相关资源
        最近更新 更多