HTTP APIs
You don’t need to make HTTP requests to build an Anvil app thanks to Anvil’s ability to call Python functions between client and server code (including code outside of Anvil). Even if you’re collaborating with a third-party, they can use the Client Uplink to safely make function calls to and from your app.
That said, HTTP is still a powerful tool to have available. Anvil has an HTTP requests library, plus a simple way to create HTTP endpoints that expose your app’s functionality over HTTP. Check the Quickstarts for this topic to get up and running.
Making HTTP Requests
You can make HTTP requests from server code by using the requests
library.
requests
library from the Python version section of the App Settings. Alternatively, it’s already included on the Standard, Data Science and Machine Learning base environments.import requests
resp = requests.get("https://api.mysite.com/foo")
print(resp.json())
For any requests that will run from client-side code, Anvil has a human-friendly Python library for making HTTP requests.
import anvil.http
resp = anvil.http.request("https://api.mysite.com/foo")
print(f"Response MIME type: {resp.content_type}")
See Making HTTP Requests for details.
Creating HTTP APIs
You can make your server functions into HTTP endpoints using a decorator:
import anvil.server
@anvil.server.route("/users/:id")
def get_user(id):
ip = anvil.server.request.remote_address
return f"You requested user {id} from IP {ip}"
See Creating HTTP Endpoints for details.
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.