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
Related articles:
Browse by tags:
Browse by language:
Popular articles:
- Web Scraping in Python - The Complete Guide
- Working with Query Parameters in Python Requests
- How to Authenticate with Bearer Tokens in Python Requests
- Building a Simple Proxy Rotator with Kotlin and Jsoup
- The Complete BeautifulSoup Cheatsheet with Examples
- The Complete Playwright Cheatsheet
- Web Scraping using ChatGPT - Complete Guide with Examples