Passing Data from Form to Anvil Server Function

Hi @Dylan,

Two parts - first, why what you’re trying isn’t working (and why we built it that way), then what you should do instead:

For security reasons, the url property you get from a Media object in a database is a temporary URL, which only works in the current browser session, and will expire with that session. If those URLs worked everywhere, forever, then you’d be giving every visitor to your app permanent access to everything they see, which probably isn’t what you wanted! We try to make the defaults safe :slight_smile:

(This is covered in the Media objects documentation, although we should probably make it a bit more prominent!)


If you want to make an image available publicly, you can return it from an HTTP endpoint. Here’s an example that uses the database row ID to find a row in a table and return its image:

@anvil.server.http_endpoint('/img/:img_id')
def get_image(img_id):
  row = app_tables.my_images.get_by_id(img_id)
  if row is not None:
    # You can check any other conditions you like here - eg whether this
    # image should be available publicly
    return row['image']

Now you can construct a URL that hits that endpoint for any row from your database:

my_row = app_tables.my_images.get(....)
url = 'https://my-app.anvil.app/_/api/img/' +
           anvil.http.url_encode(my_row.get_id())
2 Likes