【发布时间】:2016-09-16 00:02:17
【问题描述】:
我想返回一个没有内容(仅是标题)的响应,例如 this one
def show
head :ok
end
【问题讨论】:
标签: ruby-on-rails elixir phoenix-framework
我想返回一个没有内容(仅是标题)的响应,例如 this one
def show
head :ok
end
【问题讨论】:
标签: ruby-on-rails elixir phoenix-framework
您可以将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。
【讨论】:
@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
【讨论】: