Mraimou
Mraimou

Reputation: 185

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

I am using the codeigniter rest server api library.

When I enter http://localhost/RESTapi/api/question?X-API-KEY=XXX in Postman with the PUT method I'm getting: { "status": false, "error": "Invalid API key " }

It works fine with GET method How can I fix this issue?

Upvotes: 2

Views: 5086

Answers (1)

Ray
Ray

Reputation: 41428

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

Upvotes: 2

Related Questions