3

I am writing a RESTful API with a new Laravel 5.3 app.

I have a resource defined in routes/api.php like so:

Route::resource('/simple-path', 'Api\ControllerName');

There is no middleware added on the route itself, nor is there any added in the constructor of that controller, nor is there any middleware added in any parent controllers/classes.

However, Laravel(?) keeps 302 redirecting to / for any HTTP verb other than GET, and I am driving myself crazy trying to figure out why. Even if I comment out all the middleware in app/Http/Kernel.php it still redirects whenever I run a simple POST call to /simple-path in curl or PostMan.

Now, don't get me wrong, I want the auth:api middleware on my api, especially for POST or PUT requests, but I can't figure out what is causing it to redirect when there isn't any middleware running in the first place (so far as I can tell).

I've read all the docs on middleware that I can find. I am wondering if there is something other than middleware that could be causing this? This is a fairly fresh app, so I have not changed much from the basic install.

fronzee
  • 1,668
  • 2
  • 21
  • 32
  • Are you extending a controller that does controller level middleware? https://laravel.com/docs/master/controllers#controller-middleware (so within a controller method) – Chris Jan 24 '17 at 00:14
  • 1
    if you put in your routes/api.php `Route::post('/test', function(){ return response(["message" =>"test"], 200); });` and try to post to `http://yourpath.com/api/test` do you get anything? because you should get the correct response back. – AfikDeri Jan 24 '17 at 01:10
  • Its perfectly working without any errors if you have removed the middleware? – Saravanan Sampathkumar Jan 24 '17 at 02:37
  • Just do "php artisan route:list" and check which controller does post and put method uses? – Saravanan Sampathkumar Jan 24 '17 at 02:37
  • @AfikDeri your test worked, and led me to realize that my controller action was type-hinting a FormRequest subclass I had quickly set up earlier that seems to be triggering it. I had a 'required' validation rule on one of the params, and when I removed all 'required' validation rules, the request no longer redirects! – fronzee Jan 24 '17 at 16:09

2 Answers2

8

To fix this redirection, just set header parameter "Accept" to "application/json" while calling laravel api routes from rest clients, and it will work fine.

0

Going to answer my own question here. The redirection was not due to middleware but due to a FormRequest. I was type hinting a FormRequest subclass I had created in the store() function on my controller class, and this caused it to run rules() for validation first. When it saw that a required field was missing, the FormRequest class was redirecting via its own response() method. So I simply added this to my FormRequest subclass (for now):

use Illuminate\Support\Facades\Response;

public function response(array $errors)
{
    return Response::json($errors, 400);
}
fronzee
  • 1,668
  • 2
  • 21
  • 32
  • bro i have some doubts regarding the same redirection. can you pls join me on this chat room chat.stackoverflow.com/rooms/135960/laravel – Arunkumar Feb 22 '17 at 15:07