Hi Everyone,
I’ve been relying on calling anvil.server.get_api_origin() to get the path necessary to serve images over http, however recently the path returned is different from the path that is noted at the bottom of the server module. Older stored values are correct and don’t cause any issues but any new images that I create URLs for now don’t work due to the different path. Now, I can hard code the text at the bottom of the server module into the functions that generate the URLs but it seems now that there is no function available that will pull the API path for me. Has it been moved somewhere or is there some better functionality achieved by having the change in place that I need to update on? The new paths seem to include reference to ‘debug’ etc. rather than just the ‘private_api’ that was previously returned.
Any help on this is appreciated.
Thanks,
James
Hi @jamesmichaelhooker,
It looks like what you saw was a bug getting fixed, so sorry about that! The correct behaviour is that if you call get_app_origin()
or get_api_origin()
from an app in the Anvil editor, they’ll return a temporary development URL. If you’re running in the editor but you want to get the public URL of your app, call get_api_origin("published")
.
We’ve updated the docs to spell this out more clearly. Here’s the important part:
Sometimes you will want to generate URLs that point to your HTTP endpoints without having to hard-code the origin of your app. Instead of writing:
endpoint_url = "https://my-app.anvil.app/_/api/users"
You can call anvil.server.get_api_origin()
:
endpoint_url = anvil.server.get_api_origin() + "/users"
This has the advantage of returning whichever origin the user is currently connected on, i.e. if you have published a version but you are running your development version in the Anvil editor, get_api_origin()
will return a temporary private link to your development version, so you can test your APIs before publishing them!
If you’re running in the editor, get_api_origin()
might return a URL that’s private or not permanent. To get the origin for the published version of your app, call get_api_origin('published')
.
2 Likes