Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Unexpected response from PUT request
  • I'm building a RESTful API at the moment. I have base controller with a before() method that checks to see if the user is authenticated. It looks something like this:
    public function before()
    {
    parent::before();

    // Make sure the user is authenticated.
    if (!\Auth::check()
    and \Request::active()->route->segments[1] != 'login'
    and \Request::active()->action != 'unauthorized'
    and \Request::active()->action != 'logout'
    )
    {
    \Response::redirect('api/login/unauthorized');
    }
    }
    This works as expected when POSTing to or GETting from the controller when the user is not logged in. However, when I PUT to the controller (when the user is not logged in), I'm getting a 405 Method Not Allowed response. Am I doing something wrong here?
  • HarroHarro
    Accepted Answer
    You don't redirect on RESTful calls, you return an HTTP status, optionally including a message explaining the status.

    Not all RESTful clients don't support HTTP header redirect, so those are bound to fail. Besides that, what is that unauthorized method returning? If that is a different format from what the caller expects, you're in trouble. Always make sure your API is consistent.

    Having said that, a REST controller will generate a 405 if neither put_methodname and action_methodname exists. Are you sure it gets that far (check your app logs), and that it's not your webserver that is blocking the method?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion