Author avatar

Chhaya Wagmi

Explore Python Libraries: HTTPX

Chhaya Wagmi

  • Sep 15, 2020
  • 11 Min read
  • Sep 15, 2020
  • 11 Min read
Data Analytics
Machine Learning


The HTTPX library provides important features available in the Requests library as well as support for both HTTP/1.1 and HTTP/2. You can have a look at HTTPX's website to know what features HTTPX has in its pocket. In this guide, you will explore the following HTTPX topics with practical examples:

  • Passing data in URL
  • Setting custom headers
  • Accessing different forms of data
  • Posting different forms of data
  • Handling redirections and timeout

All of the code in this guide assumes that the HTTPX library has been installed and successfully imported in your current working environment as import httpx.

Passing Data in URL

Suppose you want to fetch the stock stats of a company from the Yahoo! Finance website. The associated URL will be where "ABXY" is the company's registered abbreviation. Notice that the URL consists of a key-value pair where the key is "p" and the value is "ABXY". So if you want to fetch stock stats for another company, all you need to do is replace "ABXY" with your required company's registered abbreviation.

To learn this concept practically, you can fetch the stock stats of PS (NASDAQ: Pluralsight). Start by forming a dictionary as shown:

1eq = {'p': 'PS'}

And finally, use httpx.get method and provide the complete URL including the eq variable:

1r = httpx.get(''+eq['p']+'/key-statistics', params=eq)

You can view the resulted URL using url attribute as shown:


Setting Custom Headers

If you want to let the client and server pass additional data with an HTTP request, you can set custom HTTP headers using HTTPX library. For instance, consider YouTube whose headers can be extracted as shown:

1url = ''
2r = httpx.get(url) 

The result of the above code is the following YouTube headers:

1Headers([('x-frame-options', 'SAMEORIGIN'), ('strict-transport-security', 'max-age=31536000'), ('expires', 'Tue, 27 Apr 1971 19:44:06 GMT'), ('x-content-type-options', 'nosniff'), ('content-encoding', 'gzip'), ('p3p', 'CP="This is not a P3P policy! See for more info."'), ('content-type', 'text/html; charset=utf-8'), ('cache-control', 'no-cache'), ('date', 'Sat, 27 Jun 2020 07:17:08 GMT'), ('server', 'YouTube Frontend Proxy'), ('x-xss-protection', '0'), ('set-cookie', 'YSC=QxCnrEi_49U; path=/;; secure; httponly; samesite=None'), ('set-cookie', 'VISITOR_INFO1_LIVE=L4qLJ2-3U18; path=/;; secure; expires=Thu, 24-Dec-2020 07:17:08 GMT; httponly; samesite=None'), ('set-cookie', 'GPS=1; path=/;; expires=Sat, 27-Jun-2020 07:47:08 GMT'), ('alt-svc', 'h3-27=":443"; ma=2592000,h3-25=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'), ('transfer-encoding', 'chunked')])

Notice, the default value of the content-encoding header is gzip. What if you want to go with any other alternative like br?

To update the value of a header, store the new value in a dictionary and pass the dictionary to the headers argument of the get method as shown:

1headers = {'accept-encoding': 'br'}
2print(httpx.get(url, headers=headers).headers) # The updated content encoding is br 

This updated header is shown below:

3('content-encoding', 'br'), 

Accessing Different Forms of Data

You can use the HTTPX library to fetch text, image, and JSON data. In this section, you will learn to fetch text and image data both small and large in size. For larger data you can perform streaming, which will be explained further in this section.

1. Fetching Text Data

Suppose you want to fetch data in the HTML format from a webpage that consists of not more than 5-10 lines of text as shown in the image:

UCI ML database

To do so, pass the URL inside the httpx.get method and later use the text attribute as shown:

2fetch = httpx.get('')


3 <head>
4  <title>Index of /ml/machine-learning-databases/00436</title>
5 </head>
6 <body>
7<h1>Index of /ml/machine-learning-databases/00436</h1>
8<ul><li><a href="/ml/machine-learning-databases/"> Parent Directory</a></li>
9<li><a href="Container_Crane_Controller_Data_Set.csv"> Container_Crane_Controller_Data_Set.csv</a></li>
11<address>Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips SVN/1.7.14 Phusion_Passenger/4.0.53 mod_perl/2.0.11 Perl/v5.16.3 Server at Port 443</address>

2. Streaming Text Data

To fetch large text data, you can perform streaming, i.e. fetch a limited amount of data at a time until you reach the end of the page. To understand this topic, suppose you want to extract all the text data from the Wikipedia page about Ghostbusters. To do so, you could fetch data line by line inside a for loop that is nested inside the method as shown:

1with"GET", "") as r:
2    count = 0
3    for line in r.iter_lines():
4#         print(line)
5        count += 1
7# 1166

In the above code, the print(line) statement is commented. You can uncomment it and observe the output as it streams. The last line print(count) describes the total number of lines (1166) fetched during the stream.

3. Fetching Image Data

The process of fetching an image data is similar to that of a text data. However, the only difference is how you read it once you get it in your Python environment. Consider that you need to fetch a penguin image from the Pixabay website. To do so, pass the image URL inside the httpx.get method and later use PIL and io methods to read and display the data as shown:

1from PIL import Image
2from io import BytesIO
5g = httpx.get('')

Output image Fetching image data using HTTPX

4. Streaming Image Data

You can use the method to fetch large image data. The only difference from that of streaming text data is that this time the for loop iterates over iter_bytes as shown:

1with"GET", "") as r:    
2    for data in r.iter_bytes():
3        print(data)

Posting Different Forms of Data

HTTPX can be used to post data inside a form available on a webpage. In this section, you will learn how to upload text and file data to a webpage.

1. Uploading Text Data

Suppose you want to write "Hello, World!" on a webpage using HTTPX. The input area would look like the one as shown here in the green color:

Input Text Area

To do so, use the post method and provide the information inside the data argument as shown:

1data = {'htmlString': 'Hello, World!'}
2 # Locate a correct node, for me it is "htmlString"
3r ="<URL>", data=data)

The above code results in an HTML output. Since the output is too big, given below is the properly formatted webpage with the "Hello, World!" string in the desired box:

Text Uploaded

2. Uploading a File

There are many websites where you have to upload a file on their server. Here's one instance as highlighted in the green box:

File upload

To upload a complete file rather than just writing a string, you can use the files argument of the post method. Assume you have a HTML file upload.html stored on your drive. You can upload it on this webpage using the given code:

1file = {'htmlFile': open('upload.html', 'rb')}
2 # Locate a correct node, for me it is "htmlFile"
3r ="<URL>", files=file)

You can verify that the data has been successfully uploaded by checking the console result.

Handling Redirections and Timeout

Have you ever noticed that when you type "" in a browser, it may not necessarily retain its URL as-it-is? There are good chances that you may be redirected based on the geographical region from where you are trying to access the website. For instance, run the following code and check your output:

1r = httpx.get('')

For me it is, but for you, the last two characters may differ. But, the point is the original URL has been changed and you are redirected to a new one. However, the good news is that you can control whether redirection should take place or not. If you don't need a redirection from your original URL then use allow_redirects=False inside the get method as shown:

1r = httpx.get('', allow_redirects=False)

Also, you can control how long should it take for a website to load before the program throws a timeout error. You can specify custom timeout value by using the timeout parameter as shown:

1r = httpx.get('', allow_redirects=False, timeout=1)

In the above code, if the URL is not reached before one second, the program will result in a ConnectError.


You have now learned the basics of HTTPX library by exploring its get, post, and stream methods.