0
votes

I am using Phil Sturgeon’s Rest Server and Rest Client to buid codeigniter restful api. however I am encountering the following error message:

============================================= REST Test

============================================= Request

http://localhost/mb/api/data/50

Response

A PHP Error was encountered

Severity: Notice

Message: Undefined index: base64

Filename: libraries/REST_Controller.php

Line Number: 1866

Backtrace:

File: D:\xampp\htdocs\MB\application\libraries\REST_Controller.php
Line: 1866
Function: _error_handler File: D:\xampp\htdocs\MB\application\libraries\REST_Controller.php
Line: 2005
Function: _check_login File: D:\xampp\htdocs\MB\application\libraries\REST_Controller.php
Line: 519
Function: _prepare_mb_auth File: D:\xampp\htdocs\MB\application\controllers\API.php
Line: 10
Function: __construct File: D:\xampp\htdocs\MB\index.php
Line: 292
Function: require_once

not authorized{"status":false,"error":"Not authorized"}

Call details

2
what is your authorization is it basic? If yes, have you provided Authorization header? Authorization: Basic YWRtaW46MTExMQ== NOTE:YWRtaW46MTExMQ== is base64 encode of admin:1111 - Calvin
@Calvin yes it is basic - Vincent Kituyi
@Calvin kindly explain to me exactly where to place authorization header - Vincent Kituyi
You access the link via browser? - Calvin

2 Answers

0
votes

Provide a http header when accessing API Link
Authorization: Basic YWRtaW46MTExMQ==

YWRtaW46MTExMQ== is the passphrase encoded in base64
if decoded it is

admin:1111

admin is your username 1111 is your password
Website to encode decode base https://www.base64encode.org/

You can use postman https://www.getpostman.com/ to provide Authorization header or
When accessing link on browser you can fill the alert box with username and password

You can find your username and password at application/config/rest.php
find a config of $config[rest_valid_logins]=

0
votes

If you want to disable authentication; it's simple: goto config/rest.php Change; $config['rest_auth'] = 'basic'; to $config['rest_auth'] = ''; Else, in header of your API request you have to send AUTH credential listed in: $config['rest_valid_logins'] = array('admin' => '1234');

NOTE:If you facing problem, you should change your .htaccess as following:

    <IfModule mod_rewrite.c>

        Options +FollowSymLinks
        RewriteEngine on
        SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
        #RewriteRule ^(.*)$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
        #RewriteRule ^(.*)$ - [E=REMOTE_USER:%{HTTP:Authorization},L]
        #RewriteCond %{HTTP:Authorization} ^(.*)
        #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|asset|robots\.txt)
        RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]

    </IfModule>

If you still has problem, change code of libraries\REST_Controller.php

Find this in your code:

    elseif ($this->input->server('HTTP_AUTHENTICATION')) 
    { 
      if (strpos(strtolower($this->input->server('HTTP_AUTHENTICATION')), 'basic') === 0) 
      {
          list($username, $password) = explode(':', base64_decode(substr($this->input->server('HTTP_AUTHORIZATION'), 6)));
      }
    }

and replace with:

    elseif ( $this->input->server('HTTP_AUTHENTICATION') || $this->input->server('REDIRECT_REMOTE_USER') || $this->input->server('REDIRECT_HTTP_AUTHORIZATION') )
                  {

                    $HTTP_SERVER_AUTH = ($this->input->server('HTTP_AUTHENTICATION')) ? $this->input->server('HTTP_AUTHENTICATION') : $this->input->server('REDIRECT_HTTP_AUTHORIZATION'); 
                   if(!$HTTP_SERVER_AUTH)
                   {
                     $HTTP_SERVER_AUTH = $this->input->server('REDIRECT_REMOTE_USER'); 
                   }


                    if (strpos(strtolower($HTTP_SERVER_AUTH),'basic') === 0)
                    {
                        list($username,$password) = explode(':',base64_decode(substr($HTTP_SERVER_AUTH, 6)));
                    }

                }