Laravel 5.4 POST到API重定向302而不是返回验证错误

时间:2017-05-14 09:11:58

标签: php laravel api

我正在尝试POST到我的API,但由于某种原因,所有POST请求都返回302. GET请求似乎没问题。我不明白为什么我会得到302。

api.php

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

控制器:

<?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']);
    }
}

请求验证器

<?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'],
        ];
    }
}

路线:

+--------+----------+----------------------+----------------------+----------------------------------------------------------+--------------+
| 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          |

请求&amp;响应

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>%     

点击路线时的响应示例calculator.index显示GET请求正常工作:

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

我死了并在dd('mytest')方法中转储CalculatorValuationRequest::rules(),这样就行了,所以看起来好像验证失败时,Laravel正试图重定向而不是返回422并带有验证响应。

如何让验证程序实际返回错误而不是尝试重定向用户以获取API请求?

3 个答案:

答案 0 :(得分:12)

使用Postman访问端点时,请设置标题

Accept: application/json

或Laravel永远不会知道它是一个API客户端,因此重定向302消息。 它是最新Laravel版本的机制,因为它们在web.php路由器旁边提供了api.php路由器。

答案 1 :(得分:4)

好吧,我深入挖掘了一下 - 尽管设置了内容类型,laravel由于某种原因无法分辨 - 尽管通过POSTMAN / CURL命中API路由它是一个AJAX请求。

因此,为了解决此问题,您需要指定接受标头或在请求中设置以下一个/所有标头:

  • X-Requested-With
  • Accept - 告诉我们可以接受json。

请参阅\Illuminate\Http\Concerns\InteractsWithContentTypes::expectsJson了解其运作方式。

答案 2 :(得分:3)

Laravel需要multinom标头来检测ajax请求。