Python工具 - HTTP命令行客户端 - HTTPie

一、简介

HTTPie是一个命令行下的类似CURL的HTTP请求工具,使用Python开发,相比CURL而言参数更精简、人性化,通过http命令很容易发送一些日常的http请求。

HTTPie的安装可参考:https://github.com/jakubroztocil/httpie,安装完成后即可使用http命令

二、http命令

Usage: http [flags] [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]

唯一不可缺少的是URL参数,其他参数都有一些默认行为,请求示例:

$ http -v itopic.org
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: itopic.org
User-Agent: HTTPie/1.0.2

HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 185
Content-Type: text/html
Date: Tue, 12 Mar 2019 13:52:14 GMT
Location: https://itopic.org/
Server: nginx/1.15.3

2.1 常用flags定义

2.2 METHOD定义

可选的METHOD参数,支持GETPOSTPUTDELETE等。 当没有指定该参数时,如果有数据传递则会设置为POST,否则会设置为GET,如:

http -v itopic.org from=httpie
POST / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 18
Content-Type: application/json
Host: itopic.org
User-Agent: HTTPie/1.0.2

{
    "from": "httpie"
}

2.3 URL定义

支持http://https://,默认不指定时为http://,如果是localhost也可以省略为http :/demo 等同于 http http://localhost/demo

2.4 REQUEST_ITEM 定义

curl里通过-H Content-Type:application/json来指定HTTP头部信息,-d来指定POST数据信息。而http命令里主要根据一些简单的符号来区分如何传递数据,配合示例就一清二楚了。

序号 符号 说明
2.4.1 : HTTP头部信息
2.4.2 == URL参数
2.4.3 = 键值对,用来传输json数据或者表单数据。默认会当json处理,指定-f时会当表单数据提交。
2.4.4 := 非JSON字符串类型的数据。如:awesome:=true amount:=42 colors:='["red", "green", "blue"]'。只在json传输起作用。
2.4.5 @ 上传文件

2.4.1 指定头部示例

$ http -v itopic.org X-Powered-By:itopic
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: itopic.org
User-Agent: HTTPie/1.0.2
X-Powered-By: itopic

2.4.2 设置Query参数

$ http -v itopic.org from==tools
GET /?from=tools HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: itopic.org
User-Agent: HTTPie/1.0.2

2.4.3 设置请求数据

不指定类型时默认为json数据类型。因为有提交数据,默认请求会用POST方式请求,也可以显示指定。

$ http -v itopic.org from=tools name=httpie
POST / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 35
Content-Type: application/json
Host: itopic.org
User-Agent: HTTPie/1.0.2

{
    "from": "tools",
    "name": "httpie"
}

指定-f(--form)时以表单类型提交。

$ http -f -v itopic.org from=tools name=httpie
POST / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 22
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: itopic.org
User-Agent: HTTPie/1.0.2

from=tools&name=httpie

2.4.4 设置JSON数组

因为http默认以JSON格式传递,所以省去了显示-j(--json)指定。通过下面的方式可以指定一些简单的json格式。

$ http -v itopic.org from:='["app", "web"]' name=itopic show:=true
POST / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 56
Content-Type: application/json
Host: itopic.org
User-Agent: HTTPie/1.0.2

{
    "from": [
        "app",
        "web"
    ],
    "name": "itopic",
    "show": true
}
-- EOF --
最后更新于: 2019-08-28 21:35
发表于: 2016-02-04 21:28
标签: Python 常用软件