Programmer's exclusive WeChat red envelope cover 1000, redemption code: dWK7fUs2WQG ![]()
cURL believe that many do development, operation and maintenance are not familiar, is a very useful tool terminal request, it can be carried out by means of the command line HTTP , the FTP and other requests, in Linux system The application is very wide. However, it currently has a flaw, the syntax is complex, and it is very difficult to get started. It does not even directly support the JSON parameter request. guy recently discovered another similar tool , a very simple HTTP command line client, even a bit cool.
httpie
httpie is written with Python , the supported operating system is very comprehensive, and it is very fast to get started. According to the official description, its main features are:
- Simple syntax
- Support for formatted output and color styles
- Windows , Linux , MacOS are supported
- Supports both HTTP and HTTPS
- Support file upload
- Support persistent session retention
- Built-in JSON support, support similar to Wget download
- Plugin support
VS cURL
Since it is used to benchmark cURL , let's visually see the difference between them
For the same request, cURL more parameter items and cannot be understood intuitively, while httpie more user-friendly.
Install
Four installation methods are introduced here.
PyPI
platform, as long as there is a 161f24a0897d5c Python3.7+ environment.
# 安装
pip install httpie
Windows
Install it with Chocolatey, the package manager recommended by Fat Brother.
# 安装
choco install httpie
# 升级版本
choco upgrade httpie
MacOS
There should be no developers on Apple who do not install Homebrew , right?
brew update
# 安装
brew install httpie
# 升级版本
brew upgrade httpie
Linux
Linux can be more, we commonly used Debian series, such as Ubuntu
apt update
# 安装
apt install httpie
# 升级版本
apt upgrade httpie
If it is a Red Hat system, it is natural to use yum :
yum install epel-release
# 安装
yum install httpie
# 升级版本
yum upgrade httpie
Of course, Fedora may also be used by some people dnf
command, I am not sure.
usage
httpie usage is very simple, so simple that I am too lazy to write detailed instructions, but I still have to write it. Let's try Hello World first:
The format of the httpie
https|http [flags] [METHOD] URL [ITEM [ITEM]]
For details, please http --help
for details.
request method
httpie request method (http method) is optional, httpie on their own judgment.
http pie.dev/get
unless you explicitly state:
http POST pie.dev/get
And the following method will be considered a POST request:
http pie.dev/post hello=world
Because hello=world
will be considered as the request body. GET
do you think it is 061f24a0897f8e?
does not work even if explicitly declared as GET ! **The correct way is to replace =
==
.
query string parameters
Why replace =
==
?
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 =
. becomes:
http https://api.github.com/search/repositories q==httpie per_page==1
Fixed parameter with file
Some configuration items, such as JWT Token, are old and old, and what should I do if I want to reuse them? Write to the file, and then use the @
symbol plus the path to reference the value in the file:
http POST pie.dev/post \
Authentication:@files/jwt.txt # 从文件里读取请求头
token==@files/text.txt # 从文件读取query参数
name=@files/text.txt # 请求体参数
bookmarks:=@files/data.json # 从文件中嵌入请求体json数据
In this way, I think that some configurations are made dynamic, and the value in the file can be changed.
:
request headers.
JSON
Use --json, -j
explicitly set the request Accept
to application/json
. At this time, the key-value pair connected by =
json
.
http -j PUT pie.dev/put name=felord age=18
Verify it:
-v
is--verbose
and can print request details.
If you do not use -j
, you need to use :=
to separate the key-value pair, and add @
if there is a file reference.
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.
nested
The nested format is also easy to understand. I don't think it needs to be described too much, just look at the picture below to understand.
some tricks
You only need the following way 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 Download:
http POST example.com/upload < ~/upload.pdf
http GET example.com/download.pdf > ~/download.pdf
# form 上传
http -f POST example.com/form-with-file myUpload@~/example.pdf
in addition
In addition, there are advanced gameplays such as agents and plug-ins, which are very playable and need to be explored by themselves. Based on the space, I will not repeat them. httpie actually has a UI client, but it is currently in the beta testing stage, and applications are not open.
: Felordcn for more information
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。