邮递员因未知原因而被重定向并且路线不起作用/Codeigniter 4

发布于 2025-01-10 11:50:54 字数 2180 浏览 0 评论 0原文

我可以看到邮递员由于某种原因被重定向。我刚刚开始使用邮递员,所以不确定正常的请求会是什么样子。当我提出以下要求时;

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2025-01-17 11:50:54

问题是您本质上是在 Postman (< code>_method PUT),但文件 app/Config/Routes.php 中的路由定义响应 POST 请求($routes->post('注册', 'Users::userRegister');)。

要解决此问题,请从 Postman 中删除 _method POST 请求参数:

POST https://development.example.com/Api/Register
form-data
KEY name
VALUE Thomas

附录

您的问题中看似 POST Postman 请求正在被隐式解释由于 HTTP 方法欺骗,Codeigniter 框架将其作为 PUT 请求。

HTTP 方法欺骗

使用 HTML 表单时,您只能使用 GET 或 POST HTTP 动词。
大多数情况下,这样就可以了。但是,为了支持 REST-ful 路由
您需要支持其他更正确的动词,例如 DELETE 或 PUT。
由于浏览器不支持这一点,CodeIgniter 为您提供了一个
欺骗正在使用的方法的方法。这使您可以制作一个
POST 请求,但告诉应用程序它应该被视为
不同的请求类型。

为了欺骗该方法,将隐藏输入添加到具有名称的表单中
_method 的。它的值是您想要请求的 HTTP 动词
是:

 
<输入类型=“隐藏”名称=“_method”值=“PUT”/>

此表单被转换为 PUT 请求,并且是真正的 PUT 请求,如下所示
就路由和 IncomingRequest 类而言。

您使用的表单必须是 POST 请求。获取请求
无法被欺骗。

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 to POST requests ($routes->post('Register', 'Users::userRegister');).

To fix this, remove the _method POST request parameter from Postman:

POST https://development.example.com/Api/Register
form-data
KEY name
VALUE Thomas

Addendum

The seemingly POST Postman request in your question is being implicitly interpreted as a PUT request by the Codeigniter framework because of HTTP Method Spoofing.

HTTP Method Spoofing

When working with HTML forms you can only use GET or POST HTTP verbs.
In most cases, this is just fine. However, to support REST-ful routing
you need to support other, more correct, verbs, like DELETE or PUT.
Since the browsers don’t support this, CodeIgniter provides you with a
way to spoof the method that is being used. This allows you to make a
POST request, but tell the application that it should be treated as a
different request type.

To spoof the method, a hidden input is added to the form with the name
of _method. It’s value is the HTTP verb that you want the request to
be:

   <form action="" method="post">
       <input type="hidden" name="_method" value="PUT" />
   </form>

This form is converted into a PUT request and is a true PUT request as
far as the routing and the IncomingRequest class are concerned.

The form that you are using must be a POST request. GET requests
cannot be spoofed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文