Phil REST server Phil Sturgeon, Codeigniter3, error messages not returned to PUT

I am using Phil Sturgeon, CI3 and POSTMAN server REST server for debugging. I am sending a PUT with the information below, however I am not receiving the expected error messages.

Here is my 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'), ) ); ?> 

Here is my method in my Api.php controller:

 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 ); } } 

This is located in my libraries folder as 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; } } 

When I put the following in POSTMAN:

 X-API-KEY 123456 first_name test email_address abc 

As a result, I get:

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

But I have to get validation errors.

As the debugging steps, I confirmed: - no auth errors - read form_validation.php - if I change:

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

to

 'message'=> 'test', 

the postman returns:

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

Any help is greatly appreciated.

+6
source share
2 answers

you should read this link,

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

if you use apikey you must install

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

also make a table in the database for storing the 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; 

insert at least 1 row into this database, the important key is only for the column, this is apikey

apikey should be 40 alphanumeric for security reasons

and again you have to read the documentation, and rest.php in the / config application

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

this login is set by default, so you must insert this login into your client request header, etc.

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

if this header doesn't work try

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

if you have a request like this before using

  $config['rest_auth'] = FALSE 

in fact you have not yet provided your api webservice

+3
source

I placed the PUT variables on the Headers tab in POSTman.

Only X-API-KEY belongs to the request header. The rest of the data (for example, email_address, first_name, etc.) should be transferred to the request body (for example, on the Body tab of POSTman).

Now everything is working correctly.

0
source

All Articles