Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 POST to API redirect 302 rather than returning validation error

Tags:

php

laravel

api

I am trying to POST to my API, but for some reason, all POST requests return a 302. GET requests seem fine. I cannot see why I am getting a 302.

Route in api.php

Route::resource('calculator', 'Api\CalculatorController', ['only' => ['index', 'store']]);

Controller:

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\CalculatorValuationRequest;

class CalculatorController extends Controller
{
    public function index()
    {
        return response()->json(['test' => 1]);
    }

    public function store(CalculatorValuationRequest $request)
    {
        return response()->json(['this is a test']);
    }
}

Request Validator

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CalculatorValuationRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'products' => ['required', 'array'],
            'products.*' => ['numeric', 'min:0.01', 'nullable'],
        ];
    }
}

Routes:

+--------+----------+----------------------+----------------------+----------------------------------------------------------+--------------+
| Domain | Method   | URI                  | Name                 | Action                                                   | Middleware   |
+--------+----------+----------------------+----------------------+----------------------------------------------------------+--------------+
|        | GET|HEAD | /                    | index                | Closure                                                  | web          |
|        | GET|HEAD | api/calculator       | calculator.index     | App\Http\Controllers\Api\CalculatorController@index      | api          |
|        | POST     | api/calculator       | calculator.store     | App\Http\Controllers\Api\CalculatorController@store      | api          |
|        | POST     | api/contact          |                      | App\Http\Controllers\Api\ContactController@postContact   | api          |

Request & Response

curl -X POST \  
  http://localhost:8000/api/calculator \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{"products": ["1" => "this is a test", "7" => "3"]}'
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="1;url=http://localhost:8000" />

        <title>Redirecting to http://localhost:8000</title>
    </head>
    <body>
        Redirecting to <a href="http://localhost:8000">http://localhost:8000</a>.
    </body>
</html>%     

Example response when hitting route calculator.index showing GET requests work fine:

curl -X GET \
   http://localhost:8000/api/calculator \
   -H 'cache-control: no-cache' \
   -H 'content-type: application/json' \
   -d '{"name": "asdf"}'
{"test":1}%   

I die and dump dd('mytest') inside the CalculatorValuationRequest::rules() method, and that works, so it appears as though when Validation fails, Laravel is trying to redirect rather than return 422 with validation response.

How do I get the validator to actually return an error rather than trying to redirect the user for API requests?

like image 667
Gravy Avatar asked May 14 '17 09:05

Gravy


1 Answers

When visiting the endpoint using Postman, set the Header

Accept: application/json

or Laravel would never know it's an API client and thus redirect with 302 message. It's the mechanism for latest Laravel version since they provide api.php router beside web.php router.

like image 93
roman Avatar answered Oct 05 '22 23:10

roman