【问题标题】:Phil Sturgeon's REST server, Codeigniter3, error messages no return on PUTPhil Sturgeon 的 REST 服务器 Codeigniter3,PUT 上没有返回错误消息
【发布时间】:2016-04-17 01:04:18
【问题描述】:

我正在使用 Phil Sturgeon 的 REST 服务器、CI3 和 POSTMAN 进行调试。我发送了一个包含以下信息的 PUT,但是,我没有收到预期的错误消息。

这是我的 form_validation.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
  'student_put' => array(
    array('field' => 'email_address', 'label' => 'email_address', 'rules' => 'trim|required|valid_email'),
    array('field' => 'password', 'label' => 'password', 'rules' => 'trim|required|min_length[8]|max_length[16]'),
    array('field' => 'first_name', 'label' => 'first_name', 'rules' => 'trim|required|max_length[50]'),
    array('field' => 'last_name', 'label' => 'last_name', 'rules' => 'trim|required|max_length[50]'),
    array('field' => 'phone_number', 'label' => 'phone_number', 'rules' => 'trim|required|alpha_dash'),
  )
);

?>

这是我在控制器 Api.php 中的方法:

function student_put(){
    $this->form_validation->set_data($this->put());
    // these are the rules set in config/form_validation.php
    if ($this->form_validation->run('student_put') != FALSE) {
        die('good data');
    } else { 
        $this->response( 
            array(
                'status'=> 'failure', 
                'message'=> $this->form_validation->get_errors_as_array(),
                ), 
            REST_Controller::HTTP_BAD_REQUEST  
        );
    }
}

这是在我的库文件夹中的 MY_Form_validation.php:

<?php

class MY_Form_validation extends CI_Form_validation {

  function __construct($rules = array()) {
      parent::__construct($rules);
      $this->ci =& get_instance();
  }

  public function get_errors_as_array() {
      return $this->_error_array;
  }

  public function get_config_rules() {
      return $this->_config_rules;
  }

  public function get_field_names($form) {
      $field_names = array();
      $rules = $this->get_config_rules();
      $rules = $rules[$form];
      foreach ($rules as $index=> $info) {
          $field_names[] = $info['field'];
      }
      return $field_names;
  }
}

当我在 POSTMAN 中添加以下内容时:

X-API-KEY          123456
first_name         test
email_address      abc

这是我得到的结果:

{
  "status": "failure",
  "message": []
}

但我应该得到验证错误。

作为调试步骤,我已经确认: - 没有身份验证错误 - form_validation.php 正在被读取 - 如果我改变:

'message'=> $this->form_validation->get_errors_as_array(),

'message'=> 'test',

邮递员返回:

{
"status": "failure",
"message": "test"
}

非常感谢任何帮助。

【问题讨论】:

  • application/config/rest.php 中的设置是什么?
  • 您具体指的是哪个设置。我想我应该避免将输入文件发布得太长。
  • $config['rest_auth'] 和 $config['rest_enable_keys'] 的设置是什么?
  • 我在上面运行键表中的键。 $config['rest_auth'] = FALSE;$config['rest_enable_keys'] = TRUE;$config['rest_key_column'] = 'key';当我在没有 X-API-KEY 的情况下运行 PUT 时,我得到了预期的 403 错误。当我插入正确的 X-API-KEY 时,我得到 200。
  • 刚发现我可能收不到PUT提交的数据。我使用 POSTMAN 并制作了一个 KEY/VALUE 对 first_name 和 'Richard'。我假设我可以捕捉到这样的日期: $data = array('email_address' => $this->put('email_address')); ... 或 ... 像这样回显: print_r($this->put('email_address'));不幸的是,没有捕获或显示任何内容。任何帮助表示赞赏。

标签: php codeigniter rest codeigniter-restserver


【解决方案1】:

你必须阅读这个链接,

http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814

如果使用apikey,则必须设置

    $config['rest_auth'] = 'basic'
    $config['rest_enable_keys'] = TRUE;

还要在数据库中建立一个表来存储 api key

CREATE TABLE `keys` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) NOT NULL,
    `key` VARCHAR(40) NOT NULL,
    `level` INT(2) NOT NULL,
    `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
    `is_private_key` TINYINT(1)  NOT NULL DEFAULT '0',
    `ip_addresses` TEXT NULL DEFAULT NULL,
    `date_created` INT(11) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在该数据库中插入至少 1 行,重要的列唯一键,它是 apikey

出于安全原因,apikey 必须包含 40 位字母数字

再一次,您必须阅读文档,以及 application/config 中的 rest.php

    $config['rest_valid_logins'] = ['admin' => '1234'];

该登录名是默认设置的,因此您必须将该登录名插入到客户端请求的标头等中

    http_user           'admin'
    http_pass           '1234'
    X-API-KEY           '123456'
    first_name          test
    email_address       abc

如果标题不起作用,试试这个

    http_user           'admin'
    http_pass           '1234'
    api_name            'X-API-KEY'
    api_key             '123456'
    first_name          test
    email_address       abc

如果你之前有这样的尝试请求

    $config['rest_auth'] = FALSE

实际上你还没有保护你的 api 网络服务

【讨论】:

  • 也许你没有看到我最后的评论?我没有身份验证错误。我可以使用 GET + localhost:8000/Api/student/1 仅使用密钥从我的数据库中获取数据(如果我更改密钥,我确实会收到 403 错误。使用正确的我会得到 200)。我遇到的问题是 PUT(不是身份验证)。收到的响应:{“status”:“failure”,“message”:[]} 仅供参考,是的,我正在使用密钥表等。仅供参考,rest.php 声明密钥最多 40 个字符。在我的情况下,它可以少 6 个字符。在配置中,有一个设置,Allow Authentication 和 API Keys 意味着 FALSE 对 Keys 和 TRUE 对两者。
  • 您的意思是为了使用 PUT,我必须使用 KEYS,并且至少必须使用 BASIS auth?
  • 这是我正在使用的代码的相同副本:github.com/alexmarton/RControl 只需下载并替换新的 codeigniter 副本中的文件。代码来自教程。
  • 您必须在所有类型的活动获取、发布、放置、删除中使用身份验证,因此,这种称为 REST(表示状态转移)的 Web 服务技术
  • 我使用 GET 只会 KEYS 并且它工作正常。请告诉我文档中需要身份验证的位置(例如,不仅仅是密钥)。
【解决方案2】:

我将 PUT 变量放在 POSTman 的“标题”选项卡中。

只有 X-API-KEY 属于请求标头。其余数据(例如 email_address、first_name 等)应在请求正文中传递(例如,来自 POSTman 的 Body 选项卡中)。

现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2016-02-04
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多