http vs curl

http不仅是个协议,还是一个命令。

Httpie 是个 HTTP 的命令行客户端,http就是它的命令。

1. Httpie 是什么

Httpie (aych-tee-tee-pie)是一个 HTTP 的命令行客户端。其目标是让 CLI 和 web 服务之间的交互尽可能的人性化。你可以用它很方便的用 http 的命令调试接口,最常用的应该就是 GET 和 POST 了。

2. 安装

Linux上安装

  • Debian, Ubuntu或Linux Mint
    sudo apt-get install httpie

  • Fedora,CentOS/RHEL
    sudo yum install httpie

或者使用python的方式来安装
sudo pip install --upgrade httpie

  • Mac OSX
    brew install httpie

3. http command 能做什么

curl能做的,大部分都能做。

HTTPie 基于 python 编写,内部使用了 Requests 和 Pygments 库。

HTTPie 的用法要比 cURL 直观很多,没有那么多选项,基本上心里怎么想就怎么写,默认输入和输出都是 json 格式 (而 cURL 必须要指定 -H “Content-Type: application/json”)。我们同样实现上面获取 token 的功能,效果如下:

  • example 1:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ➜  tmp http https://www.devops-tech.cn/
    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Access-Control-Allow-Origin: *
    Age: 7
    Cache-Control: max-age=600
    Connection: keep-alive
    Content-Encoding: gzip
    Content-Length: 2510
    Content-Type: text/html; charset=utf-8
    Date: Wed, 19 Jun 2019 08:28:44 GMT
    ETag: W/"5d0848b1-2d66"
    ...

others

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
http

HTTPie: HTTP client, a user-friendly cURL replacement.

- Download a URL to a file:
http -d example.org

- Send form-encoded data:
http -f example.org name='bob' profile_picture@'bob.png'

- Send JSON object:
http example.org name='bob'

- Specify an HTTP method:
http HEAD example.org

- Include an extra header:
http example.org X-MyHeader:123

- Pass a user name and password for server authentication:
http -a username:password example.org

- Specify raw request body via stdin:
cat data.txt | http PUT example.org

4. 特点:

1、直观的语法
2、格式化和色彩化的终端输出
3、内置 JSON 支持
4、支持上传表单和文件
5、HTTPS、代理和认证支持
6、支持任意请求数据
7、自定义标题
8、持久性会话
9、类 Wget 下载
10、支持 Python 2.6, 2.7 和 3.x
11、支持 Linux, Mac OS X 和 Windows
12、插件
13、文档
14、测试覆盖率

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~