CURL is a terminal request tool that can be used to make HTTP and FTP requests on the command line. It is widely used in Linux. However, it has one drawback, the syntax is complex, it is quite difficult to get started, and it does not even directly support JSON parameter requests at present. He recently discovered httpie, a very simple HTTP command line client that’s a bit cool.
httpie
Httpie is written in Python and supports a very comprehensive operating system. It is very quick to use, and it can be used in 5 minutes. According to the official description, its main features are:
- Grammar is simple
- Supports formatted output and color styles
- Windows, Linux, and MacOS are supported
- Both HTTP and HTTPS are supported
- Support file upload
- Supports persistent session persistence
- Built-in JSON support, support similar to Wget download
- Support plugins
VS cURL
With cURL, let’s see what the difference is
The same request, the parameters of the cURL a more intuitive and can’t understand, and httpie more humanized.
The installation
There are four installation methods.
PyPI
Regardless of the platform, as long as the **Python3.7+** environment is available.
# installation
pip install httpie
Copy the code
Windows
Install it with Chocolatey, the package manager that Chubby recommended.
# installation
choco install httpie
# Upgrade version
choco upgrade httpie
Copy the code
MacOS
There are no developers on Apple who don’t have Homebrew installed, right?
brew update
# installation
brew install httpie
# Upgrade version
brew upgrade httpie
Copy the code
Linux
Linux can be more, we commonly used Debian system, such as Ubuntu
apt update
# installation
apt install httpie
# Upgrade version
apt upgrade httpie
Copy the code
Yum yum yum yum yum yum
yum install epel-release
# installation
yum install httpie
# Upgrade version
yum upgrade httpie
Copy the code
Of course Fedora might also be a favorite. Try using the DNF command. I’m not sure.
usage
Httpie is easy to use, so simple that I won’t bother to elaborate on it, but I’ll do it anyway. Let’s try Hello World:
The format is as follows:
https|http [flags] [METHOD] URL [ITEM [ITEM]]
Copy the code
You can query details through HTTP –help.
Request method
Httpie’s HTTP method is optional, and httpie decides for itself.
http pie.dev/get
Copy the code
Unless you explicitly state:
http POST pie.dev/get
Copy the code
The following method is considered a POST request:
http pie.dev/post hello=world
Copy the code
Because hello=world is considered the body of the request. So why do you think it’s GET?
Even if explicitly declared as GET! ** The correct way to do this is to replace = with ==.
Query string parameters
Why do I replace theta with theta = theta?
https://api.github.com/search/repositories?q=httpie&per_page=1
The above is a standard API format in Httpie? Both and & are replaced with Spaces, and arguments do not need URL escape. The query argument key-value pair uses ==; The request body parameter key-value pair uses =. To:
http https://api.github.com/search/repositories q==httpie per_page==1
Copy the code
Fix parameters with files
Some configuration items, such as JWT Token, are too old and I still want to reuse them. Write to a file, and then use the @ sign and path to reference the value in the file:
http POST pie.dev/post \
Authentication:@files/jwt.txt Read the request header from the file
token==@files/text.txt Read the query parameter from the file
name=@files/text.txt Request body parameters
bookmarks:=@files/data.json Embed request body JSON data from the file
Copy the code
This way I think some configuration dynamic, change the value in the file is ok.
Request header for:.
JSON
Using –json, -j explicitly sets the request Accept to application/json, when the = connected key-value pairs are converted to JSON.
http -j PUT pie.dev/put name=felord age=18
Copy the code
Verify:
-V, short for –verbose, prints request details.
Instead of -j, use := to separate key/value pairs, plus @ if there are file references.
http PUT pie.dev/put \
name=John \ # String (default)
age:=29 \ # Raw JSON -number
married:=false \ # Raw JSON -- Boolean
hobbies:='["http", "pies"]' \ # Raw JSON -array
favorite:='{"tool": "HTTPie"}' \ # Raw JSON -- Object
bookmarks:=@files/data.json \ # Embed JSON file
description=@files/text.txt # Embed text file
Copy the code
The actual request body JSON is:
{
"age": 29."bookmarks": {
"httpie": {
"says": "Hello, World!"}},"description": "Hello, World! \n"."favorite": {
"tool": "HTTPie"
},
"hobbies": [
"http"."pies"]."married": false."name": "John"
}
Copy the code
In this case = is the same as :=.
nested
Nested formats are also understandable. I don’t think it needs too much description, just look at the picture below.
Some skills
Just need this way to quickly request
# https://baidu.com
https ://baidu.com
Copy the code
If localhost is used, it can be simplified as:
# https://localhost:8080/yourapi
https :8080/yourapi
Copy the code
Upload and download:
http POST example.com/upload < ~/upload.pdf
http GET example.com/download.pdf > ~/download.pdf
# upload form
http -f POST example.com/form-with-file myUpload@~/example.pdf
Copy the code
In addition
In addition, there are agents, plug-ins and other advanced gameplay, playability is very strong, need to grope, based on the length will not repeat. Httpie actually has a UI client, but it’s not open for applications currently in beta.