【问题标题】:Laravel 5.4 POST to API redirect 302 rather than returning validation errorLaravel 5.4 POST 到 API 重定向 302 而不是返回验证错误
【发布时间】:2017-10-13 05:15:40
【问题描述】:

我正在尝试 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          |

请求与响应

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

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

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

【问题讨论】:

    标签: php laravel api


    【解决方案1】:

    使用 Postman 访问端点时,设置 Header

    Accept: application/json

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

    【讨论】:

      【解决方案2】:

      Laravel 需要 {"X-Requested-With":"XMLHttpRequest"} 标头来检测 ajax 请求。

      【讨论】:

      • 谢谢!我正在测试 Airlock 并收到 302 未经身份验证的呼叫。添加这个解决了我的问题,现在按预期得到 401。
      【解决方案3】:

      好的,我挖得更深了——尽管设置了内容类型,但 laravel 出于某种原因无法分辨——尽管通过 POSTMAN / CURL 访问了 API 路由,它是 AJAX 请求。

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

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

      请参阅\Illuminate\Http\Concerns\InteractsWithContentTypes::expectsJson 了解其工作原理。

      【讨论】:

        猜你喜欢
        • 2023-01-21
        • 1970-01-01
        • 2020-07-26
        • 1970-01-01
        • 1970-01-01
        • 2015-11-14
        • 2016-09-15
        • 2017-11-30
        • 1970-01-01
        相关资源
        最近更新 更多