liveBook · Manning

Requests

As explained in Build Python Web Apps with Streamlit

requests is a Python library that provides a user-friendly interface for sending HTTP requests and handling their responses.

A Streamlit app may need information from another website, such as a daily joke or movie details. `requests` is a Python library for sending HTTP requests to an API and working with its responses. It provides a simpler interface than handling HTTP with Python alone. section 5.3.2 section 11.1.1

A basic API call has a few parts:

  • Install the library with `pip install requests`; use `pip show requests` to check the installation. section 5.3.2
  • Call `requests.get(url)` to send a GET request. The returned `Response` holds the server’s reply; `response.text` reads its body as a string. section 5.3.2
  • Pass a dictionary through `headers` when an API requires a header such as `X-Api-Key`, or through `params` when it expects URL query parameters. section 5.3.2 section 11.1.1

A missing key is useful feedback

A joke API request without its required key returns `<Response [400]>` and `{"error": "Missing API Key."}` in `response.text`. Supply the key in the `X-Api-Key` header, and the response instead contains a joke. Keep real credentials out of committed code. section 5.3.2 section 5.5

>>> import requests
>>> params = {'api_key': 'Replace with your API key', 'query': 'ferris bueller'}
>>> response = requests.get('https://api.themoviedb.org/3/search/movie', params=params)
>>> response.json()

For a movie search, `params` carries the API key and search term. `response.json()` reads the JSON reply as Python data; its `results` list contains movie records. section 11.1.1

For deployment, list `requests` in `requirements.txt` alongside other dependencies. You can specify a compatible version or pin an exact one so a difference between development and production does not break the app. Store API credentials separately: use `st.secrets` locally and configure production secrets in Streamlit Community Cloud. section 5.4.1 section 5.5

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through GET requests, headers, query parameters, and response reading; leaves broader library details to its documentation.
Examples
Uses a joke API’s missing-key error and successful reply, then a movie-search query and its JSON results.
What it assumes
Assumes basic Python shell and pip use while explaining HTTP requests and responses in plain language.
What you can do after
Apply it via `requests.get` to query an API, provide required parameters or headers, and read text or JSON responses.
How it connects
Links HTTP APIs and JSON responses to Streamlit deployment, dependency management, and credential storage.

Also explained in

Compare how 6 books explain requests →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.