天天看點

python筆記42-http請求指令行工具(httpie)前言環境安裝發送GET請求POST請求json檔案導入—help檢視配置參數

前言

通常我們需要快速的測試某個接口通不通,一般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: [email protected]
License: BSD
Location: e:\python36\lib\site-packages
Requires: requests, colorama, Pygments
Required-by:           

複制

發送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": "[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:\Users\dell>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