Master HTTP Requests with Python: Easy Introduction to Httpie 🚀
Today, I want to introduce you to an HTTP tool library that I frequently use in my daily development — httpie. As a Python developer, we often need to debug API interfaces and test HTTP requests. While GUI tools like Postman are powerful, sometimes it’s more convenient and quicker to send requests directly from the terminal. Httpie is an elegant command-line HTTP client that makes sending HTTP requests as natural as chatting with an old friend!
Learning Objectives
-
Master the basic installation and usage of httpie -
Learn to send various HTTP requests (GET, POST, PUT, etc.) -
Familiarize yourself with setting request parameters and headers -
Be able to handle file uploads and JSON data
Prerequisites
-
Basic command-line operations -
Fundamentals of the HTTP protocol -
Usage of the Python package management tool pip
Core Content
1. Installation and Configuration
First, install httpie using pip:
pip install httpie
Once installed, we can use the http
command. Let’s try the simplest GET request:
http httpbin.org/get
2. Basic Usage
GET Request
Send a GET request with parameters:
http GET 'httpbin.org/get' name==Wang role==teacher
POST Request
Send JSON data:
http POST 'httpbin.org/post' name=Wang age=18 hobby:='["Programming", "Reading"]'
Tip: Use
:=
to send JSON values; httpie will automatically handle the data types.
3. Advanced Techniques
Custom Request Headers
Send a request with custom headers:
http 'httpbin.org/headers' User-Agent:Python-Demo API-Key:123456
File Upload
Upload a file:
http -f POST 'httpbin.org/post' file@~/test.txt
Authentication Request
Send an authenticated request:
http -a username:password 'httpbin.org/basic-auth/username/password'
4. Practical Features
-
Syntax highlighting: Automatically displays responses in beautiful colors. -
Formatted output: JSON responses are automatically formatted. -
File Download: Use the --download
parameter. -
Session Management: Use --session
to save authentication information.
Practical Case
Let’s look at a complete API testing process:
# 1. First, get the token
http POST 'api.example.com/login' username=demo password=123456
# 2. Use the token to access protected resources
http GET 'api.example.com/users' Authorization:'Bearer abc123'
# 3. Upload a file with additional parameters
http -f POST 'api.example.com/upload' file@./data.csv category=report
Interactive Section
Exercises
-
Use httpie to send a POST request to httpbin.org/anything
containing the following data:-
name: Your Name -
age: Your Age -
skills: An array containing at least 3 skills
-
Challenge Task
Implement a simple weather query tool:
-
Register for a weather API account -
Use httpie to write a script to query the weather in your city -
Parse the returned JSON data and display it in a formatted way
Summary
Today we learned:
-
Installation and basic usage of httpie -
Methods for sending various HTTP requests -
Handling request parameters and headers -
File uploads and authentication requests -
Useful debugging techniques
Next Episode Preview
We will dive deeper into Python’s requests library, which is the core library behind httpie, allowing us to elegantly handle HTTP requests in code!
If you have any questions, feel free to ask in the comments. I’m Wang, see you next time!
Learning Check-in: Did you learn to send requests with httpie today? Share your experiences in the comments!
⭐ Like and bookmark; your support is my motivation for creating!