Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

def show
  head :ok
end
like image 257
domp Avatar asked May 19 '16 08:05

domp


People also ask

What is plug in Phoenix?

Plug is a specification for composable modules in between web applications. It is also an abstraction layer for connection adapters of different web servers. The basic idea of Plug is to unify the concept of a "connection" that we operate on.

Is Phoenix better than Rails?

There is no winner in the Rails vs. Phoenix battle when it comes to their syntax; both frameworks are on an equal footing here. Phoenix syntax feels a lot like the syntax of Ruby on Rails. Both are clean and understandable.


1 Answers

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.

like image 68
Dogbert Avatar answered Oct 08 '22 01:10

Dogbert