Laravel - 测试 API 时出现错误 405,但从邮递员调用时则不会

发布于 2025-01-09 07:46:34 字数 1913 浏览 1 评论 0原文

我正在尝试使用 API 中某个控制器的 update 操作。它使用 PUT http 方法。在邮递员中,它工作正常,返回状态 200 和我的 Json 响应,如预期:

在此处输入图像描述

当我编写 Laravel 自动化测试时,相同的请求返回 405(不允许的方法)错误。我尝试了 dumpHeaders 并发现 Allow 标头不接受 PUT 请求:

在此处输入图像描述

但是 PUT 端点是正确定义,并在我的本地主机中工作(请参阅上面的邮递员请求)。其他人似乎也有类似的问题,但我没有找到任何回应。

编辑:这是我的api路由(/routes/api.php):

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

// Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
//     return $request->user();
// });

Route::post('/login', [AuthController::class, 'login'])->name('apiLogin');
// Route::post("/register", [AuthController::class, "register"]);

Route::middleware('jwt.verify')->group(function ($router) {
    // Route::post("/logout", [AuthController::class, "logout"]);
    Route::post('/refresh', [AuthController::class, 'refresh']);
    Route::get('/user-profile', [AuthController::class, 'userProfile']);

    Route::apiResources([
        [...other routes identical to the following...]
        'eselo-comarcas' => EseloComarcasController::class,
        [...other routes identical to the previous...]
    ]);
}

我唯一能想象的是接受的Http请求方法有一些配置,这在本地主机开发环境中是不同的在测试/生产环境中。有人可以帮助我吗?

I'm trying to use the update action from some controller in my API. It uses the PUT http method. In postman, it works fine, returning a status 200 and my Json response, as expected:

enter image description here

When I write a Laravel automated test, the same request returns a 405 (Method Not Allowed) error. I tried dumpHeaders and discovered the Allow header doesn't accept PUT requests:

enter image description here

But the PUT endpoint is correctly defined, and working in my local host (see postman request above). Other people seems to have similar problems, but I found no response.

EDIT: Here are my api routes (/routes/api.php):

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

// Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
//     return $request->user();
// });

Route::post('/login', [AuthController::class, 'login'])->name('apiLogin');
// Route::post("/register", [AuthController::class, "register"]);

Route::middleware('jwt.verify')->group(function ($router) {
    // Route::post("/logout", [AuthController::class, "logout"]);
    Route::post('/refresh', [AuthController::class, 'refresh']);
    Route::get('/user-profile', [AuthController::class, 'userProfile']);

    Route::apiResources([
        [...other routes identical to the following...]
        'eselo-comarcas' => EseloComarcasController::class,
        [...other routes identical to the previous...]
    ]);
}

The only thing I can imagine is there is some configuration on accepted Http request methods which is different in local host development environment and in test/production environment. Can somebody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文