azharmalik3
azharmalik3

Reputation: 563

Phoenix return 200 status code for POST

My Phoenix API returns 200 status code for POST request instead of 201. Phoenix default uses 200 if I am not set the status code.

Here is sample response.

conn |> json(%{created_at: response[:timestamp], notes: response[:notes], data: data})

Upvotes: 16

Views: 7888

Answers (1)

Dogbert
Dogbert

Reputation: 222040

You can set the status code manually using Plug.Conn.put_status/2:

conn
|> put_status(:created)
|> json(%{created_at: response[:timestamp], notes: response[:notes], data: data})

Phoenix's phoenix.gen.json task does the same: https://github.com/phoenixframework/phoenix/blob/dd8ce7bd65bd8749e901349d5789bcb94a95521b/priv/templates/phoenix.gen.json/controller.ex#L17.

Upvotes: 24

Related Questions