我正在使用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;
}
}
当我把以下放在波斯曼:
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"
}
非常感谢您的帮助。
您必须阅读此链接,
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密钥
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 Web服务
我把PUT变量放在POSTman的Headers选项卡中。
只有X-API-KEY属于请求头中。其余数据(例如email_address、first_name等)应在请求正文中传递(例如从POSTman的正文选项卡中传递)。
现在一切正常。