When making HTTP requests in Python using the popular requests library, the scheme (http:// or https://) is usually specified in the URL. However, sometimes you may want to make a request in a scheme-agnostic way without hardcoding http or https.
This can be useful in situations where:
Fortunately, the
Constructing Scheme-Agnostic URLs
Instead of hardcoding the scheme like:
url = "https://example.com/api"
You can omit it:
url = "//example.com/api"
This constructs a scheme-relative URL that will use the default scheme determined by the request context at runtime.
Making Requests
Once you have your scheme-agnostic URL, making the actual request works the same:
import requests
url = "//example.com/api"
response = requests.get(url, timeout=5)
print(response.status_code)
The request will default to either HTTP or HTTPS based on factors like if the host URL supports HTTPS, environment variables, etc.
Wrap Up
Constructing scheme-relative URLs and allowing