python笔记42-http请求命令行工具(httpie)

前言

通常我们需要快速的测试某个接口通不通,一般linux上用curl去发http请求,但是这个命令行工具语法有点复杂了,不够直观。
python有一个给人类使用的requests库,非常的简单方便。httpie就是基于requests开发的,给人类用的命令行工具,取代curl的绝佳工具。

环境安装

pip install httpie==1.0.3

查看版本号

C:\Users\dell>pip show httpie
Name: httpie
Version: 1.0.3
Summary: HTTPie - a CLI, cURL-like tool for humans.
Home-page: http://httpie.org/
Author: Jakub Roztocil
Author-email: jakub@roztocil.co
License: BSD
Location: e:\python36\lib\site-packages
Requires: requests, colorama, Pygments
Required-by:

C:\Users\dell>

发送GET请求

get请求不需要带body参数,所以不带参数,会被默认识别成get请求

http http://127.0.0.1:8000/info

访问后查看结果,就是这么高效!

C:\Users\dell>http http://127.0.0.1:8000/info
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 290
Content-Type: application/json
Date: Wed, 18 Sep 2019 14:21:25 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

[
    {
        "age": 20,
        "create_time": "2019-09-15",
        "id": 1,
        "mail": "283340479@qq.com",
        "name": "yoyo",
        "sex": "M"
    },
    {
        "age": 444444,
        "create_time": "2019-09-18",
        "id": 6,
        "mail": "283340479@qq.com",
        "name": "yoyo",
        "sex": "M"
    }
]

POST请求

GET请求是默认不带body部分的,那么带上body部分的参数,肯定会识别成POST请求,所以也不用声明请求类型。
一般接口是json类型的,所以头部请求参数类型Content-Type默认是application/json

接下来发个POST请求,比如我要发送的报文是这样的

POST http://127.0.0.1:8000/info HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: HTTPie/1.0.3
Accept-Encoding: gzip, deflate
Accept: application/json, */*
Connection: keep-alive
Content-Type: application/json
Content-Length: 63

{"name": "yoyo", "sex": "M", "age": "20", "mail": "123@qq.com"}

那么用httpie的命令行只需下面简单的一行,如果参数是字符串,可以用key=value格式,如果参数不是字符串,那么用key:=value

http http://127.0.0.1:8000/info name=yoyo sex=M age=20 mail=123@qq.com

C:\Users\dell>http http://127.0.0.1:8000/info name=yoyo sex=M age=20 mail=123@qq.com
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 95
Content-Type: application/json
Date: Wed, 18 Sep 2019 14:22:22 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "data": {
        "age": "20",
        "mail": "123@qq.com",
        "name": "yoyo",
        "sex": "M"
    },
    "message": "create some data!"
}

json文件导入

如果json的参数较多,可以把请求参数写到一个json文件,如test.json

{
    "name": "yoyo",
    "sex": "M",
    "age": "20",
    "mail": "123@qq.com"
}

接下来发请求的时候,导入这个json文件就可以

http http://127.0.0.1:8000/info < test.json

测试结果

D:\>http http://127.0.0.1:8000/info < test.json
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 95
Content-Type: application/json
Date: Wed, 18 Sep 2019 14:46:36 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "data": {
        "age": "20",
        "mail": "123@qq.com",
        "name": "yoyo",
        "sex": "M"
    },
    "message": "create some data!"
}

--help查看配置参数

使用--help查看更多配置参数

http --help

显示详细的请求,显示请求的头部和返回的内容

http -v

只显示Header

http -h

只显示Body

http -b

下载文件

http -d

使用http代理

http --proxy=http:http://xxx:x

posted @ 2019-09-18 22:55 上海-悠悠 阅读(...) 评论(...) 编辑 收藏