Search code examples
ruby-on-railsrubyhttphttp-status-codes

HTTP status code for this case (400 or 422)


I have some method that looks like this

def some_method
   if params["status"] == "good"
      some action
      render json: {message: "action success"}, status: 200
   else 
      render json: {message: "The status is not good, try again later when the status is good"}, status: 200
   end
end

You see when the status is not good, currently i'm returning 200 status code, what is the best status code for this case. Is it 400 or 422?


Solution

  • When I build API, usually I use 422 aka :unprocessable_entity for validation errors.

    In my opinion, :unprocessable_entity looks more explicit.