Codeigniter 3 + Restserver始终执行get请求

x3naxklr  于 8个月前  发布在  其他
关注(0)|答案(2)|浏览(57)

我正在使用 Codeigniter 3.1.0Restserverhere下载,* 文档 * 来自here。我也在使用Chrome的扩展程序 Postman
问题是,即使我从Postman的下拉菜单中选择POST,它也会命中get方法。下面是代码:

defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';

class Example extends REST_Controller {
    function __construct() {
        parent::__construct();
    }
    public function users_get() {
        echo "get request";
    }
    public function users_post() {
        echo "post request";
    }
}

现在,通过Postman,如果我选择 GET 到URL example-domain.com/API/example/users,则预览为getrequest
如果我选择 POST 到相同的URL example-domain.com/API/example/users,则预览再次为get request而不是post request
我没有在 config/rest.php 中做任何更改,我使用的是 controllers/API/example 中的Restserver实现示例
有谁知道为什么我不能使用POST方法?

tvz2xvvm

tvz2xvvm1#

最后,我找到了问题的原因。我以前在这个域上安装了SSL,但我试图用HTTP调用API。
在.htaccess中,我有rewriterule
RewriteRule ^ https://% {HTTP_HOST}%{REQUEST_URI} [L,R=301]
强制使用HTTPS。
如果我用HTTPS发出POST请求,它就像一个魅力。
如果我用HTTP发出POST请求,它会重定向到HTTPS(因为重写规则),因此会有一个新的GET请求指向新页面。

g0czyy6m

g0czyy6m2#

使用此

RewriteEngine On

# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# CodeIgniter URL Rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

相关问题