首页 文章

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

提问于
浏览
4

我试图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>%

点击路线计算器时的响应示例显示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请求?

3 回答

  • 3

    使用Postman访问 endpoints 时,请设置 Headers

    Accept: application/json

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

  • 9

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

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

    • X-Requested-With

    • Accept - 告诉我们可以接受json .

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

  • 1

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

相关问题