Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

[v2.0.17] Example of API custom endpoint not working #762

Closed
gnh1201 opened this issue Feb 11, 2019 · 17 comments
Closed

[v2.0.17] Example of API custom endpoint not working #762

gnh1201 opened this issue Feb 11, 2019 · 17 comments
Assignees
Labels
bug Something isn't working

Comments

@gnh1201
Copy link

gnh1201 commented Feb 11, 2019

Steps to Reproduce

  1. I read the tutorial.
    https://docs.directus.io/extensions/custom-endpoints.html#creating-custom-endpoints

  2. I changed the name of the file _example.php in the /public/extensions/custom/endpoints/ directory to example.php.

  3. I tested the endpoint.

$ curl http://directus.local/custom/example
{"error":{"error":8,"message":"API Environment Configuration Not Found: custom"}}
$ curl http://directus.local/example
{"error":{"error":8,"message":"API Environment Configuration Not Found: example"}}
$ curl http:/directus.local/_/example
{"error":{"code":1,"message":"Not Found"}}
$ curl http://directus.local/_/custom/example
{"error":{"code":1,"message":"Not Found"}}

Can I see what I did wrong?

@gnh1201 gnh1201 changed the title Example of API custom endpoint not working [v2.0.17] Example of API custom endpoint not working Feb 11, 2019
@wellingguzman
Copy link
Contributor

Thanks @gnh1201 for reporting this, I am going to give this a look.

@wellingguzman
Copy link
Contributor

I wasn't unable to reproduce it either on 2.0.17 nor master. Can you try testing this on the master branch? renaming _example.php to example.php should work. and accessing the endpoint should be curl http://directus.local/_/custom/example

In the other hand, I am experiencing this bug, when the path doesn't exists:

{
    "error": {
        "code": null,
        "message": "Too few arguments to function Directus\\Application\\ErrorHandlers\\NotFoundHandler::__invoke(), 2 passed in /Users/welling/src/api/vendor/slim/slim/Slim/App.php on line 530 and exactly 3 expected",
        "class": "ArgumentCountError",
        "file": "/Users/welling/src/api/src/core/Directus/Application/ErrorHandlers/NotFoundHandler.php",
        "line": 19
    }
}

@benhaynes benhaynes added the not reproducible The developer couldn’t reproduce the bug label Feb 11, 2019
@yarick2000
Copy link
Contributor

yarick2000 commented Feb 11, 2019

Hello, I also was trying to make a call to example custom endpoint which code is located in public\extensions\custom\endpoints_example.php file. I've renamed _example.php to example.php and was trying to make an authenticated GET call to the following route: /myproject/custom/example.
Basing on the code inside of this file I should get a response with the following array:
['item 1','item 2']
But instead I've got an error:

Error stack
{
"error": {
"code": null,
"message": "Internal Server Error"
}
}
And in logs I see the following error:
[2019-02-11 13:51:29] api[casino].ERROR: ArgumentCountError: Too few arguments to function Directus\Application\ErrorHandlers\NotFoundHandler::__invoke(), 2 passed in D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php on line 531 and exactly 3 expected in D:\Git\github\directus\api\src\core\Directus\Application\ErrorHandlers\NotFoundHandler.php:19
Stack trace:
#0 D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php(531): Directus\Application\ErrorHandlers\NotFoundHandler->__invoke
#1 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\AbstractRateLimitMiddleware.php(34): Slim\App->__invoke
#2 [internal function]: Directus\Application\Http\Middleware\AbstractRateLimitMiddleware->__invoke
#3 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#4 [internal function]: Slim\DeferredCallable->__invoke
#5 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#6 D:\Git\github\directus\api\vendor\wellingguzman\proxy-detection\src\ProxyDetectionMiddleware.php(30): Slim\App->Slim{closure}
#7 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\ProxyMiddleware.php(18): RKA\Middleware\ProxyDetectionMiddleware->__invoke
#8 [internal function]: Directus\Application\Http\Middleware\ProxyMiddleware->__invoke
#9 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#10 [internal function]: Slim\DeferredCallable->__invoke
#11 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#12 D:\Git\github\directus\api\vendor\akrabat\ip-address-middleware\src\IpAddress.php(113): Slim\App->Slim{closure}
#13 [internal function]: RKA\Middleware\IpAddress->__invoke
#14 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#15 [internal function]: Slim\DeferredCallable->__invoke
#16 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#17 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\CorsMiddleware.php(66): Slim\App->Slim{closure}
#18 [internal function]: Directus\Application\Http\Middleware\CorsMiddleware->__invoke
#19 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#20 [internal function]: Slim\DeferredCallable->__invoke
#21 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#22 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\ResponseCacheMiddleware.php(47): Slim\App->Slim{closure}
#23 [internal function]: Directus\Application\Http\Middleware\ResponseCacheMiddleware->__invoke
#24 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#25 [internal function]: Slim\DeferredCallable->__invoke
#26 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#27 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim{closure}
#28 D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php(407): Slim\App->callMiddlewareStack
#29 D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php(315): Slim\App->process
#30 D:\Git\github\directus\api\src\core\Directus\Application\Application.php(161): Slim\App->run
#31 D:\Git\github\directus\api\public\index.php(5): Directus\Application\Application->run [] []

The API version is 2.0.17
I have API instance running on my local Windows 10 machine with XAMPP 3.2 installed.
I was using the latest build from https://github.com/directus/api.git.
Calls to core endpoints working without any problem.

@wellingguzman
Copy link
Contributor

@yarick2000 I am aware of that one, I will be fixing it soon. Although it seems you are not using the correct path to the endpoint. If you go to http://yourhost/<project>/custom/example you should be able to see the response below:

{
    "data": [
        "item 1",
        "item 2"
    ]
}

If you request an unknown endpoint even adding trailing slash http://yourhost/<project>/custom/example/ it will throw that error, otherwise you shouldn't be seeing the error.

Can you show me a screenshot of the request (url, headers, responses, etc) or the raw http request?

@yarick2000
Copy link
Contributor

yarick2000 commented Feb 11, 2019

Sorry that I just gave a route and not the full url. Here is what I was using:
http://directus.pinnacle.com:82/myproject/custom/example,
where is directus.pinnacle.com:82 is the local domain and port for API.
Here is a full request and response:
Request Headers:
cache-control:"no-cache"
Authorization:"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNTQ5OTIwODE3LCJ0eXBlIjoiYXV0aCIsImtleSI6Ijc4MzMzODMzLTg2ZDctNDYwNC1iM2Q3LWRhMDgwNmM5NTkxMCIsInByb2plY3QiOiJjYXNpbm8ifQ._JK8BZQGa8r7CV1zXPRihK4oF9n72MUje97XwqGWNg0"
User-Agent:"PostmanRuntime/7.6.0"
Accept:"/"
Host:"directus.pinnacle.com:82"
cookie:"PHPSESSID=af968mncog2ctjtf8p29cucdaj"
accept-encoding:"gzip, deflate"
Response Headers:
Date:"Mon, 11 Feb 2019 21:28:37 GMT"
Server:"Apache/2.4.37 (Win32) OpenSSL/1.1.1a PHP/7.3.0"
X-Powered-By:"PHP/7.3.0"
Access-Control-Allow-Origin:"*"
Content-Length:"57"
Connection:"close"
Content-Type:"application/json;charset=utf-8"
Response Body:
error:
code:null
message:"Internal Server Error"

wellingguzman added a commit that referenced this issue Feb 11, 2019
@wellingguzman
Copy link
Contributor

From your request/response, I can't tell where is the actual path of the request being made or the actual response (in json).

I made a fix for the invalid third argument. At least that one should be fixed. If you were getting that error is likely you are going to get a 404 not found error.

Ref: 93b53bf

@yarick2000
Copy link
Contributor

So what other information you need from me in order to get more details about the issue? I see the changes you've made and understand they not fixing the issue but the error handler...

@yarick2000
Copy link
Contributor

Hey, sorry, I've figured out that directory endpoints are working for me, so the issue only with file endpoints (if file located right under custom/endpoints folder, like example.php)

@benhaynes
Copy link
Sponsor Member

@wellingguzman — is there something we can add to the docs or example file to make this clearer?

@wellingguzman
Copy link
Contributor

@benhaynes I think there's should be a issue on loading the files apparently, I don't think there's nothing we can make more clear about it. Well, because I don't actually know what the issue is here.

@yarick2000 can you list the files in custom/endpoints. I see you are making the request using postman, can you click below the "Send" button the "code" button, and pick http from the top left list and share the raw http request (make sure to remove any sensitive information).

Also when you get the response back from the server click on the "headers" tab above the response "view".

You can do this and share the screenshots for both directory and files endpoints, that way we can see the differences between both requests and help us narrow down the issue.

@yarick2000
Copy link
Contributor

yarick2000 commented Feb 13, 2019

Sorry, I didn't found the Generate Code Snippet there, but I can give a raw request/response print:
For directory:
GET /myproject/custom/directory
cache-control: no-cache
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNTUwMDY5NjQxLCJ0eXBlIjoiYXV0aCIsImtleSI6Ijc4MzMzODMzLTg2ZDctNDYwNC1iM2Q3LWRhMDgwNmM5NTkxMCIsInByb2plY3QiOiJjYXNpbm8ifQ.EN7cFV8zWZNQNf67DFwphp6vfPe78TgrIazUD3Z_sfc
User-Agent: PostmanRuntime/7.6.0
Accept: /
Host: myhost.com:82
cookie: PHPSESSID=af968mncog2ctjtf8p29cucdaj
accept-encoding: gzip, deflate
HTTP/1.1 200
status: 200
Date: Wed, 13 Feb 2019 14:49:02 GMT
Server: Apache/2.4.37 (Win32) OpenSSL/1.1.1a PHP/7.3.0
X-Powered-By: PHP/7.3.0
Access-Control-Allow-Origin: *
Content-Length: 28
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: application/json;charset=utf-8
{"data":["item 1","item 2"]}

For file endpoint (example.php):
GET /myproject/custom/example
cache-control: no-cache
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNTUwMDcwMDkzLCJ0eXBlIjoiYXV0aCIsImtleSI6Ijc4MzMzODMzLTg2ZDctNDYwNC1iM2Q3LWRhMDgwNmM5NTkxMCIsInByb2plY3QiOiJjYXNpbm8ifQ.-cXztv1Jf-wDjDVgE_7BJAgSZ_iSdKFls2CecxtyYiE
User-Agent: PostmanRuntime/7.6.0
Accept: /
Host: myhost.com:82
cookie: PHPSESSID=af968mncog2ctjtf8p29cucdaj
accept-encoding: gzip, deflate
HTTP/1.1 500
status: 500
Date: Wed, 13 Feb 2019 14:56:33 GMT
Server: Apache/2.4.37 (Win32) OpenSSL/1.1.1a PHP/7.3.0
X-Powered-By: PHP/7.3.0
Access-Control-Allow-Origin: *
Content-Length: 57
Connection: close
Content-Type: application/json;charset=utf-8
{"error":{"code":null,"message":"Internal Server Error"}}

Except of example.php there is also .htaccess file under public/extensions/custom/endpoints and the folder with name 'directory'

@wellingguzman
Copy link
Contributor

@yarick2000 you are still getting "Internal Server Error". can you share what's on your logs directory?

Are you still getting the error below or is it something new?

Too few arguments to function Directus\\Application\\ErrorHandlers\\NotFoundHandler::__invoke(), 2 passed in /Users/welling/src/api/vendor/slim/slim/Slim/App.php on line 530 and exactly 3 expected

@yarick2000
Copy link
Contributor

I didn't change the version - I'm still on 2.0.17, so yes, I'm getting Internal Server Error. Was 2.0.18 already released?
This is what I have in logs for now:
[2019-02-13 09:56:34] api[casino].ERROR: ArgumentCountError: Too few arguments to function Directus\Application\ErrorHandlers\NotFoundHandler::__invoke(), 2 passed in D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php on line 531 and exactly 3 expected in D:\Git\github\directus\api\src\core\Directus\Application\ErrorHandlers\NotFoundHandler.php:19
Stack trace:
#0 D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php(531): Directus\Application\ErrorHandlers\NotFoundHandler->__invoke
#1 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\AbstractRateLimitMiddleware.php(34): Slim\App->__invoke
#2 [internal function]: Directus\Application\Http\Middleware\AbstractRateLimitMiddleware->__invoke
#3 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#4 [internal function]: Slim\DeferredCallable->__invoke
#5 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#6 D:\Git\github\directus\api\vendor\wellingguzman\proxy-detection\src\ProxyDetectionMiddleware.php(30): Slim\App->Slim{closure}
#7 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\ProxyMiddleware.php(18): RKA\Middleware\ProxyDetectionMiddleware->__invoke
#8 [internal function]: Directus\Application\Http\Middleware\ProxyMiddleware->__invoke
#9 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#10 [internal function]: Slim\DeferredCallable->__invoke
#11 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#12 D:\Git\github\directus\api\vendor\akrabat\ip-address-middleware\src\IpAddress.php(113): Slim\App->Slim{closure}
#13 [internal function]: RKA\Middleware\IpAddress->__invoke
#14 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#15 [internal function]: Slim\DeferredCallable->__invoke
#16 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#17 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\CorsMiddleware.php(66): Slim\App->Slim{closure}
#18 [internal function]: Directus\Application\Http\Middleware\CorsMiddleware->__invoke
#19 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#20 [internal function]: Slim\DeferredCallable->__invoke
#21 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#22 D:\Git\github\directus\api\src\core\Directus\Application\Http\Middleware\ResponseCacheMiddleware.php(47): Slim\App->Slim{closure}
#23 [internal function]: Directus\Application\Http\Middleware\ResponseCacheMiddleware->__invoke
#24 D:\Git\github\directus\api\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array
#25 [internal function]: Slim\DeferredCallable->__invoke
#26 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#27 D:\Git\github\directus\api\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim{closure}
#28 D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php(407): Slim\App->callMiddlewareStack
#29 D:\Git\github\directus\api\vendor\slim\slim\Slim\App.php(315): Slim\App->process
#30 D:\Git\github\directus\api\src\core\Directus\Application\Application.php(161): Slim\App->run
#31 D:\Git\github\directus\api\public\index.php(5): Directus\Application\Application->run [] []

@wellingguzman
Copy link
Contributor

Hey @yarick2000, can you try testing the latest from master branch? That error in particular was fixed, and I would like to know what are you getting 404.

Is the example.php file all lowercase? Maybe is a issue with case-sensitive.

@yarick2000
Copy link
Contributor

Hi, I will check the master when I will have time. Since I succeed with directory based endpoints this issue became less important for me. The name of example.php is lower cased - I just removed the underscore.

@binal-7span
Copy link
Contributor

Seems it is working fine on Mac and Linux systems, but not on Windows. The issue might be related to the .htaccess file.

@theharshin theharshin added bug Something isn't working and removed not reproducible The developer couldn’t reproduce the bug labels Apr 18, 2019
@binal-7span binal-7span self-assigned this May 17, 2019
@binal-7span
Copy link
Contributor

Fixed in #961

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants