邮递员因未知原因而被重定向并且路线不起作用/Codeigniter 4
我可以看到邮递员由于某种原因被重定向。我刚刚开始使用邮递员,所以不确定正常的请求会是什么样子。当我提出以下要求时;
POST https://development.example.com/Api/Register
form-data
KEY name
VALUE Thomas
KEY _method
VALUE PUT
由于某种原因,它返回了我网站的首页。当我查看 apache2 日志文件时,我可以看到:
124.xxx.xxx.xxx - - [27/Feb/2022:09:08:36 +0000] "POST /Api/Register HTTP/1.1" 303 5724 "-" "PostmanRuntime/7.28.4"
124.xxx.xxx.xxx - - [27/Feb/2022:09:08:36 +0000] "GET / HTTP/1.1" 200 8185 "https://development.example.com/Api/Register" "PostmanRuntime/7.28.4"
当我通过像 chrome 这样的网络浏览器访问它时(当然我无法提交任何值),我得到的结果低于预期;
{
"status": 500,
"message": {
"name": "The name field is required",
"email": "The email field is required",
"password": "The password field is required.",
"password_confirmation": "The password_confirmation field is required."
},
"error": true,
"data": []
}
请问我对 POSTMAN 做错了什么吗?
我的 Users
控制器是;
public function userRegister(){
$user = new UserEntity($this->request->getPost());
$user->startActivation();
if ($this->UserModel->insert($user)){
$this->sendActivationEmail($user);
$response =[
'status' => 200,
'message' => 'User registed. Check email to activate account.',
'error' => false,
'data' => []
];
} else {
$response =[
'status' => 500,
'message' => $this->UserModel->errors(),
'error' => true,
'data' => []
];
}
return $this->respondCreated($response);
}
同样令人困惑的是,该路线无法通过以下方式访问:
我的路线文件是;
$routes->group('Api', ["namespace" => 'App\Controllers\Api\v1'] , function($routes){
$routes->post('Register', 'Users::userRegister');
});
https://development.example.com/Api/Register
Error; Controller or its method is not found: \App\Controllers\Api\Register::index
https://development.example.com/Api/v1/Users/userRegister
This will work and give correct error like way above.
I can see that postman is being redirected for some reason. I have just started using postman so not sure what a normal request would look like. When I make the following request;
POST https://development.example.com/Api/Register
form-data
KEY name
VALUE Thomas
KEY _method
VALUE PUT
For some reason it returns my top page of my website. When I look at the apache2 log file, I can see:
124.xxx.xxx.xxx - - [27/Feb/2022:09:08:36 +0000] "POST /Api/Register HTTP/1.1" 303 5724 "-" "PostmanRuntime/7.28.4"
124.xxx.xxx.xxx - - [27/Feb/2022:09:08:36 +0000] "GET / HTTP/1.1" 200 8185 "https://development.example.com/Api/Register" "PostmanRuntime/7.28.4"
When I access it through a web browser like chrome (and I cannot submit any values of course), I get below which is expected;
{
"status": 500,
"message": {
"name": "The name field is required",
"email": "The email field is required",
"password": "The password field is required.",
"password_confirmation": "The password_confirmation field is required."
},
"error": true,
"data": []
}
What am I doing wrong with POSTMAN please?
My Users
controller is;
public function userRegister(){
$user = new UserEntity($this->request->getPost());
$user->startActivation();
if ($this->UserModel->insert($user)){
$this->sendActivationEmail($user);
$response =[
'status' => 200,
'message' => 'User registed. Check email to activate account.',
'error' => false,
'data' => []
];
} else {
$response =[
'status' => 500,
'message' => $this->UserModel->errors(),
'error' => true,
'data' => []
];
}
return $this->respondCreated($response);
}
What is also puzzling is that the route is NOT accessible via;
My routes file is;
$routes->group('Api', ["namespace" => 'App\Controllers\Api\v1'] , function($routes){
$routes->post('Register', 'Users::userRegister');
});
https://development.example.com/Api/Register
Error; Controller or its method is not found: \App\Controllers\Api\Register::index
https://development.example.com/Api/v1/Users/userRegister
This will work and give correct error like way above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您本质上是在 Postman (< code>_method PUT),但文件 app/Config/Routes.php 中的路由定义仅响应
POST
请求($routes->post('注册', 'Users::userRegister');
)。要解决此问题,请从 Postman 中删除
_method
POST 请求参数:附录
您的问题中看似
POST
Postman 请求正在被隐式解释由于 HTTP 方法欺骗,Codeigniter 框架将其作为PUT
请求。HTTP 方法欺骗
The problem is that you're essentially making a
PUT
request in Postman (_method PUT
), yet your route definition in the file app/Config/Routes.php only responds toPOST
requests ($routes->post('Register', 'Users::userRegister');
).To fix this, remove the
_method
POST request parameter from Postman:Addendum
The seemingly
POST
Postman request in your question is being implicitly interpreted as aPUT
request by the Codeigniter framework because of HTTP Method Spoofing.HTTP Method Spoofing