1000 wechat red envelope covers for programmers, exchange code: dWK7fUs2WQG
cURL believes that many developers and operators are familiar with it. It is a very useful terminal request tool. With the help of it, HTTP, FTP and other requests can be made on the command line. It is widely used in Linux system. However, it has a defect that its syntax is complex and it is very difficult to get started. It even does not directly support JSON parameter requests at present. Fat brother recently found another similar tool httpie, a very simple HTTP command-line client, which is even a little cool.
httpie
httpie is written in Python. It supports a very comprehensive operating system and can be used very quickly. Fat brother can use it after watching it for 5 minutes. According to the official description, its main features are:
- Simple grammar
- Supports formatted output and color styles
- Windows, Linux and MacOS support
- Support both HTTP and HTTPS
- Support file upload
- Support continuous session persistence
- Built in JSON support, similar to Wget Download
- Support Plug-Ins
VS cURL
Since it is used to benchmark cURL, let's take a visual look at the differences between them
For the same request, the parameter items of cURL are more and cannot be understood intuitively, while httpie is much more humanized.
install
Four installation methods are introduced here.
PyPI
Ignore the platform, as long as there is Python 3 7 + environment is enough.
# install pip install httpie
Windows
Install with the previously recommended package manager chocolate.
# install choco install httpie # Upgrade version choco upgrade httpie
MacOS
Should there be no developer on Apple who doesn't install Homebrew?
brew update # install brew install httpie # Upgrade version brew upgrade httpie
Linux
Linux is much more. We often use Debian system, such as Ubuntu
apt update # install apt install httpie # Upgrade version apt upgrade httpie
If it's red hat, it's natural to use yum:
yum install epel-release # install yum install httpie # Upgrade version yum upgrade httpie
Of course, some people in Fedora may like to use it. Try using the dnf command. I'm not sure.
usage
The usage of httpie is so simple that I don't bother to write a detailed description, but I still have to write it. First try Hello World:
The format of httpie command is:
https|http [flags] [METHOD] URL [ITEM [ITEM]]
You can query the details through http --help.
Request method
The http method of httpie is optional, and httpie will judge by itself.
http pie.dev/get
Unless you explicitly state:
http POST pie.dev/get
The following method will be considered as a POST request:
http pie.dev/post hello=world
Because hello=world will be considered as the request body. Then why do you think it's GET?
Not even if it is explicitly declared as GET** The correct approach is to replace = with = =.
Query string parameters
Why replace = with = =?
https://api.github.com/search/repositories?q=httpie&per_page=1
The above is a standard API format. In httpie? And & are replaced by spaces, and the parameters do not need URL escape. The query parameter key value pair uses = =; The request body parameter key value pair uses =. Change to:
http https://api.github.com/search/repositories q==httpie per_page==1
Fix parameters with file
Some configuration items, such as JWT Token, are old and I want to reuse them. What should I do? Write in the file, and then use the @ symbol and path to reference the value in the file:
http POST pie.dev/post \ Authentication:@files/jwt.txt # Read request header from file token==@files/text.txt # Read query parameters from file name=@files/text.txt # Request body parameters bookmarks:=@files/data.json # Embed request body json data from file
In this way, I think it's OK to dynamically change some configurations and change the values in the file.
For request header:.
JSON
Use -- json, -j to explicitly set the request Accept to application/json. At this time, the key value pair of = connection will be converted to json.
http -j PUT pie.dev/put name=felord age=18
Verify:
-v is the abbreviation of -- verbose, which can print the request details.
If you do not use - j, you need to use: = to separate key value pairs. If there is a file reference, you need to add @.
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
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" }
At this time, = and: = are the same.
nesting
The nested format is also well understood. I don't think it needs too much description. You can understand it by looking at the figure below.
Some skills
You only need the following method to quickly request
# https://baidu.com https ://baidu.com
If it is localhost, it can be simplified to:
# https://localhost:8080/yourapi https :8080/yourapi
Upload and download:
http POST example.com/upload < ~/upload.pdf http GET example.com/download.pdf > ~/download.pdf # form upload http -f POST example.com/form-with-file myUpload@~/example.pdf
in addition
In addition, there are advanced playing methods such as agents and plug-ins. They are very playable and need to explore by themselves. Based on the length, I won't repeat them. httpie actually has a UI client, but at present β During the test phase, no application was opened.
Pay attention to the official account: Felordcn for more information