python筆記42-http請求命令行工具(httpie)

  • 2019 年 10 月 6 日
  • 筆記

前言

通常我們需要快速的測試某個介面通不通,一般linux上用curl去發http請求,但是這個命令行工具語法有點複雜了,不夠直觀。 python有一個給人類使用的requests庫,非常的簡單方便。httpie就是基於requests開發的,給人類用的命令行工具,取代curl的絕佳工具。

環境安裝

pip install httpie==1.0.3

查看版本號

C:Usersdell>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: [email protected]  License: BSD  Location: e:python36libsite-packages  Requires: requests, colorama, Pygments  Required-by:

發送GET請求

get請求不需要帶body參數,所以不帶參數,會被默認識別成get請求

http http://127.0.0.1:8000/info

訪問後查看結果,就是這麼高效!

C:Usersdell>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": "[email protected]",          "name": "yoyo",          "sex": "M"      },      {          "age": 444444,          "create_time": "2019-09-18",          "id": 6,          "mail": "[email protected]",          "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": "[email protected]"}

那麼用httpie的命令行只需下面簡單的一行,如果參數是字元串,可以用key=value格式,如果參數不是字元串,那麼用key:=value

http http://127.0.0.1:8000/info name=yoyo sex=M age=20 [email protected]

C:Usersdell>http http://127.0.0.1:8000/info name=yoyo sex=M age=20 [email protected]  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": "[email protected]",          "name": "yoyo",          "sex": "M"      },      "message": "create some data!"  }

json文件導入

如果json的參數較多,可以把請求參數寫到一個json文件,如test.json

{      "name": "yoyo",      "sex": "M",      "age": "20",      "mail": "[email protected]"  }

接下來發請求的時候,導入這個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": "[email protected]",          "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