Laravel - 测试 API 时出现错误 405,但从邮递员调用时则不会
我正在尝试使用 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:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论