Note python 42-http request command line tool (httpie)

Foreword

Usually we need to quickly test an interface through unreasonable, generic linux use curl to send http request, but the syntax is somewhat complicated command-line tool, and not intuitive.
There is a python library to requests for human use, very simple and convenient. httpie is developed based on requests, to the command-line tool for human use, replace curl a great tool.

Environment Installation

pip install httpie==1.0.3

View the version number

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:

C:\Users\dell>

Send GET request

band body does not need to get request parameters, no parameters will be identified by default as a get request

http http://127.0.0.1:8000/info

After the visit to see the results, it is so efficient!

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 request

GET request with no body part is the default, then put the parameters section of the body, will certainly be recognized as a POST request, so do not declare the type of request.
Json type is generic interface, so the head of the request type parameter default Content-Type application / json

Then send a POST request, for example, I want to send a message like this

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]"}

Then use command line httpie simply following line, if the parameter is a string, key = value form may be used, if the parameter is not a string, then use 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 file import

If more json parameters can be written to a json file request parameters, such as test.json

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

Then send the request when the json file can be imported

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

Test Results

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 to view the configuration parameters

See more of the configuration parameters using the --help

http --help

It shows detailed request, the display request header and returned content

http -v

Show only Header

http -h

Show only Body

http -b

download file

http -d

Use http proxy

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

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11546176.html