CodeIgniter REST API Library Ajax PUT throwing 403 Forbidden

后端 未结 4 545
长情又很酷
长情又很酷 2021-01-02 11:51

I got the rest of the library working fully, just trying to generate api keys and its throwing a 403 forbidden when executed via ajax.

({\"status\":false,\"err

4条回答
  •  星月不相逢
    2021-01-02 12:26

    I have solved the problem of generating the api key. I'm using Phil Sturgeon's REST API server. Call the key controller using ajax call as such :

    $("#submitGetApiKey").click(function(){
        $.ajax({
            url: "http://sitename.com/api/key/index?X-API-KEY=your_key_here",
            crossDomain: true,  /* remove this if using the same domain*/
            type: "PUT",
            dataType: "jsonp",
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert(errorThrown);
            },
            success: function(data){
                for (var i = keys.length - 1; i >= 0; i--) {
                    console.log(keys[i]);
                };
            }
        });
     });
    

    Inside key controller: Search for function _generate_key() and check for $this->load->helper('security');. the security helper must be loaded for working of do_hash otherwise you will get 500 internal server error.

    public function index_put()
    {
        // Build a new key
        $key = self::_generate_key();
    
        // If no key level provided, give them a rubbish one
        $level = $this->put('level') ? $this->put('level') : 1;
        $ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
    
        // Insert the new key
        if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
        {
            $this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
        }
    
        else
        {
            $this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
        }
    }
    

    Also, you may call http://sitename.com/api/keyindex?X-API-KEY=your_key_here in your browser's address bar by making a small change in your key controller you can replace the function name index_put with index_get.

    Thanks

提交回复
热议问题