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?