Calling a Server Connect API from another SC call

We often call a server connect function from a different server connect call, by using the API Action. This works well, and allows us to reuse a complex database query and checks etc. However I’m wondering if there is a better way to do this rather than using the API Action?

The only real problem we have with this approach is it doesn’t seem possible to use localhost in the API call - we have to use either an IP address, or a FQDN. Using localhost throws a security error (even though there is no security on the server connect call to retrieve this data).

Using a fixed address has been ok for testing, and has worked fine over the past few months as we’ve just been running on a development system, however as we move to a production server, having a fixed address doesn’t work as we need to refer to different systems in each case.

It would be fine if we can use localhost, but better if there was a more ‘integrated’ way to call one server connect method from within another and I’d welcome any thoughts others have on achieving this goal.

https://{{$_SERVER['HTTP_HOST']}}/dmxConnect/api/PATH-TO-FILE.php
1 Like

Thanks. Unfortunately this seems to do the same thing as if I use localhost - I get the following error -:

"message":"Failed to connect to localhost port 8000: Connection refused"

There is no security enforcement on this API call, and as mentioned if I simply use the IP address it works :confused:.

Sorry, that wasn’t meant for localhost but rather using different domains for dev and production.

There are other server variables you can use that can get you the protocol and port (I think :thinking:)

1 Like

Ok, thanks - I’ll take a look at this.

Currently I’m running in a local Docker environment - I’d like to find a way to make these calls no-matter where the system is deployed - local or otherwise.

SERVER_ADDR works. I’m not sure why localhost or the HTTP_HOST fail - maybe it’s an issue with dns lookup that’s preventing this working. Anyway, many thanks for pointing me in the right direction here.

1 Like

Cool. Server address is probably better since it gives the ip address.

1 Like

Unfortunately I’m still plagued with this issue as when we swap to the production server, different variables work - a different set than when we run locally :frowning: . This ultimately means we have to keep swapping the API calls when we deploy to different servers which is far from ideal, and very error prone.

@George is there a recommended way to call one Server Connect API from within another? It feels like there should be to avoid duplicating complex queries etc, and I feel I’m probably missing something obvious.

This really should work. What are you seeing? Have you checked Errors in the web logs?

It gives me a 404 - URI not found on the server. If I change to IP address, or a string URI, it works on the production server, but that’s not portable to the Docker container. I can use SERVER_ADDR on the Docker container, but not on the production system… It just feels like there should be a nice way in Wappler to make a SC call internally?

Docker has a different port by default, have you tried using a server variable for that as well?

Yes, Docker has a different port, but it works fine on Docker - it’s my production server that doesn’t work when using this server variable.

The 404 error should show in the web logs, does that will give you something concrete to fix?

Here’s something that works (at least on my infrastructure)

($_SERVER.HTTPS ? 'https://' : 'http://') + $_SERVER.SERVER_NAME + '/dmxConnect/api/file2.php'

This properly calls file2.php in a local docker container (http and localhost) and also on a remote LAMP stack (https and domain name)

In putting this together I realized that IP address while more direct, doesn’t account for virtual hosts.

Thanks @mebeingken on my production system that works - I’ve not tested this in my Docker instance as I want to avoid swapping targets too often due to the bug with database connections, but if you’ve tested this then it looks like it could be a good workaround.

It still feels like this is something Wappler should do directly - ie be able to reference one SC call from another, rather than going via a separate API call. @George what do you think?

This can be done by using routes… choose server connect route then the server action you want to do… save and copy the url into the api url field on the server side. This is what worked for us.