codeigniter 休息服务器 POST "not allowed",但 GET 工作正常

codeigniter rest server POST "not allowed", but GET works fine

我正在使用 Phil Sturgeon 的 codeigniter-restserver,

https://github.com/chriskacerguis/codeigniter-restserver

这是我遇到的一个问题:

当我执行 PUT 请求时,一切正常,但是当我执行 POST 时,我得到了 “500 内部服务器错误”

<div id="container">
  <h1>An Error Was Encountered</h1>
  <p>The action you have requested is not allowed.</p>
</div>

我的代码如下:

function test_post()
{
    $this->response('ok', 200); // 200 being the HTTP response code
}

function test_get()
{
    $this->response('ok', 200); // 200 being the HTTP response code
}

可以在下面找到有效的 GET 处理 URL

https://manage.pineconetassel.com/index.php/api/example/test/

请注意,我只允许使用 https。

我用hurl.it测试了POST方法,还是不行。

这里是 rest.php 配置:

$config['force_https'] = TRUE;
$config['rest_default_format'] = 'json';
$config['rest_status_field_name'] = 'status';
$config['rest_message_field_name'] = 'error';
$config['enable_emulate_request'] = TRUE;
$config['rest_realm'] = 'REST API';
$config['rest_auth'] = false;
$config['auth_source'] = 'ldap';
$config['auth_library_class'] = '';
$config['auth_library_function'] = '';
$config['rest_valid_logins'] = array('admin' => '1234');
$config['rest_ip_whitelist_enabled'] = false;
$config['rest_ip_whitelist'] = '';
$config['rest_ip_blacklist_enabled'] = false;
$config['rest_ip_blacklist'] = '';
$config['rest_database_group'] = 'default';
$config['rest_keys_table'] = 'keys';
$config['rest_enable_keys'] = FALSE;
$config['rest_key_column'] = 'key';
$config['rest_key_length'] = 40;
$config['rest_key_name'] = 'X-API-KEY';
$config['rest_logs_table'] = 'logs';
$config['rest_enable_logging'] = FALSE;
$config['rest_access_table'] = 'access';
$config['rest_enable_access'] = FALSE;
$config['rest_logs_json_params'] = FALSE;
$config['rest_limits_table'] = 'limits';
$config['rest_enable_limits'] = FALSE;
$config['rest_ignore_http_accept'] = FALSE;
$config['rest_ajax_only'] = FALSE;

我是不是做错了什么或使用了错误的方法来测试 POST 还是我需要配置一些东西?

config/config.php

中设置$config['csrf_protection'] = FALSE;

注意它不在 config/rest.php

之下

接受的答案是正确的。 但是如果你不想禁用 $config['csrf_protection'] 以防你需要它用于 Web 表单,你需要排除 REST API URI,例如,你的 REST url 以 /api/,只需设置

$config['csrf_exclude_uris'] = array(
   'api/[a-z0-9/_-]+'
);

也在config/config.php下。我使用正则表达式来简化它。