What is the equivalent of 'head :ok' from Rails in Phoenix?

前端 未结 2 426
自闭症患者
自闭症患者 2021-01-17 11:14

I want to return a response that has no content (merely headers) like this one

def show
  head :ok
end
2条回答
  •  逝去的感伤
    2021-01-17 11:31

    You can use Plug.Conn.send_resp/3 with empty body:

    # 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 can take the status (second argument) as an integer or one of the supported atoms mentioned here: https://hexdocs.pm/plug/Plug.Conn.Status.html#code/1.

提交回复
热议问题