Enabling Cors in Codeigniter ( Restserver by @Chriskacerguis )

enabling cors in codeigniter ( restserver by @chriskacerguis )

Try adding OPTIONS to the allowed methods.

header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");

and return immediately when the request is method 'OPTIONS' once you have set the headers.

if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
die();
}

See also this answer.

Angular sends a W3C CORS spec compliant preflight request that will check for the right allowed methods before actually attempting it.

Personally, I find the Mozilla Developer Network CORS page a bit easier to read on the matter to help understand the flow of CORS.

HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js

I encountered exactly the same problem. To solve it I have a MY_REST_Controller.php in core and all my REST API controllers use it as a base class. I simply added a constructor like this to handle OPTIONS requests.

function __construct() {

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();
}
parent::__construct();
}

This just checks if the request type is OPTIONS and if so just dies out which return a code 200 for the request.

Invalid Api key using RestServer

i faced the same issue .don't mention put/get/post in URL ,RestServer itself recognizes the request nature base on parameter you pass two step required to solve your problem .

1st Step :

http://localhost/projects/myapi/key/index_put.php

must change to :

http://localhost/projects/myapi/key/index.php

2nd step:

create an api kay using sha1(max 40 character) in the keys table (table structure show in config/rest.php file),
enter 1 in is_private_key field and ::1 in ip_address field.
create the record ,and check it again .

Invalid Api key using RestServer

i faced the same issue .don't mention put/get/post in URL ,RestServer itself recognizes the request nature base on parameter you pass two step required to solve your problem .

1st Step :

http://localhost/projects/myapi/key/index_put.php

must change to :

http://localhost/projects/myapi/key/index.php

2nd step:

create an api kay using sha1(max 40 character) in the keys table (table structure show in config/rest.php file),
enter 1 in is_private_key field and ::1 in ip_address field.
create the record ,and check it again .

Invalid API key as a reponse of PUT method using RESTServer codeigniter

I've seen some API's that do not look at the GET params if you make a POST or PUT request for credentials or are inconsistent in how they do it.

Really, credentials should go in headers either via the Authorize header or a custom one for many reasons like 'not logging credentials to access logs', but I digress.

In this case you can try:

  • Put (no pun) the X-API-KEY=XXX inside the body of the PUT just to see if this works
  • See if/how the library accepts the API key in a header

Looking at this library in particular (https://github.com/chriskacerguis/codeigniter-restserver), they do support the header X-API-KEY. This should be where you put the key for ALL requests--it's best practice not to pass them as url params.

Here's the commandline example using curl from their Github project.

 curl -X POST -H "X-API-KEY: some_key_here" http://example.com/books

In PHP you can use curl to set header like this:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-KEY: XXX'));


Related Topics



Leave a reply



Submit