405 method not allowed - CodeIgniter Rest-server

4.4k views Asked by At

I am having troubles with Codeigniter - Rest Server for a week already. I have a controller called Users with 2 methods all_users_get and register_post.

  1. all_users_get is working fine.
  2. register_post

    returns { "status": false, "error": "Unknown method" }

If I change the method all_users_get for POST I get the same error, just works with GET

<?php

use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

class Users extends REST_Controller {

function __construct()
{
    // Construct the parent class
    parent::__construct();

    $this->load->model('user_model');

    // Configure limits on our controller methods
    // Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
    $this->methods['users_get']['limit'] = 500; // 500 requests per hour per user/key
    $this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key
    $this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
}

/**
 * @method: GET
 * Fetch all users
 */

public function all_users_get()
{
    $users = $this->user_model->all_users();
    // Check if the users data store contains users (in case the database result returns NULL)
    if ($users)
    {
        // Set the response and exit
        $this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
    }
    else
    {
        // Set the response and exit
        $this->response([
            'status' => FALSE,
            'message' => 'No users were found'
        ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
    }
    $this->response($users, REST_Controller::HTTP_OK);

}

/**
 * User Register
 * *************************
 * 
 * @param: fullname
 * @param: email address
 * @param: password
 * @param: username
 * 
 * *************************
 * @method: POST
 * @link : api/users/register
 */

public function resgister_post()
{

    header('Access-Control-Allow-Origin: *');

    $this->response("Teste", REST_Controller::HTTP_OK);

}

}

I am running my code in localhost with Xampp and I found on phpinfo

_SERVER["REQUEST_METHOD"] GET

I am confused I don't know if this had something to do with Xampp conf or my code, please help. Thank you all

3

There are 3 answers

0
Jason Full Stack Developer On BEST ANSWER

This function should work for you :)

  public function index_post()// post data to the database
  {
    $data = [
        'first_name'        => $this->post('first_name'),
        'last_name'         => $this->post('last_name'),
        'email'             => $this->post('email')

    ];

    if( $this->users->createUser($data) > 0)
    {
        $this->response([
            'status' => true,
            'message' => 'NEW USER CREATED'
        ], REST_Controller::HTTP_CREATED); 
    }
    else{
        $this->response([
            'status' => false,
            'message' => 'FAILED TO CREATE NEW USER'
        ], REST_Controller::HTTP_BAD_REQUEST);
    }

}
0
Obot Ernest On

Ultimate Solution Step 1 In your constructor, add the code below

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
    die();
}

Step 2: Open your route.php and define an API route e.g

$route['api/v1/auth/login'] = 'api/v1/auth/login';

Note: the route will be used in step 3

Step 3: Open your config.php, add the code below

if (stripos($_SERVER["REQUEST_URI"],'/api/v1/auth/login/') === FALSE) {
    $config['csrf_protection']  = FALSE;
}else{ 
    $config['csrf_protection']  = FALSE; 
}

Note: change /api/v1/auth/login/ to your endpoint

0
mayur prajapat On
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type');
    exit;
}

just copy above line in constructor it worked for me