What I’m trying to do:
I am trying to make a basic HTTP get request using the anvil.http module.
What I’ve tried and what’s not working:
I tried to use the following code. But it is failing with HttpError: HTTP error 0
Code Sample:
url = 'https://pastebin.mozilla.org/PKqfUNvC/raw'
self.label_trained_models.text = url
resp = anvil.http.request(url)
I even tried with anvil.http.request(url, json=True)
. But that failed too.
The browser restrictions section of the docs, Anvil Docs | Making HTTP requests , describes the situations in which you’ll get an HTTP error 0 when using anvil.http.requests
in the client.
In your case, the URL isn’t responding properly to the CORS request. The CORS request is made since you’re making a connection out to a different domain than your app is on. When the URL doesn’t respond to the request, that tells the browser that the URL isn’t designed to be accessed from other domains.
The docs describe two options for fixing the issue. In your case, since you don’t control the URL, making the request from a server function bypasses the browser restrictions.
1 Like
api.mysite.com/foo
is a placeholder, not an actual working API. If you want to test to see if it works, you need to use an API that supports CORS, e.g.:
result = anvil.http.request(url='https://goweather.herokuapp.com/weather/NewYork', json=True)