Rick Zheng's Blog

Httpie

2021-02-21

开源项目Httpie学习

概述

HTTPie (pronounced aitch-tee-tee-pie) is a command-line HTTP client. Its goal is to make CLI interaction with web services as human-friendly as possible. HTTPie is designed for testing, debugging, and generally interacting with APIs & HTTP servers. The http & https commands allow for creating and sending arbitrary HTTP requests. They use simple and natural syntax and provide formatted and colorized output.

HTTPie 操作更加友好,好像真是这样.比如CURL虽然也很强大,但是用起来不太友好…

主要功能

  • Expressive and intuitive syntax
  • Formatted and colorized terminal output
  • Built-in JSON support
  • Forms and file uploads
  • HTTPS, proxies, and authentication
  • Arbitrary request data
  • Custom headers
  • Persistent sessions
  • Wget-like downloads
  • Linux, macOS and Windows support
  • Plugins
  • Documentation
  • Test coverage

安装

centos 安装为例

yum install httpie

raise error

ImportError: cannot import name UnrewindableBodyError

python2 -m pip uninstall urllib3
python2 -m pip install urllib3==1.21.1

文档建议使用Python3安装

python3 -m pip install httpie

实践

basic

request & response

` http -v http://httpbin.org/get `

http http://httpbin.org/get

only header

http -h http://httpbin.org/get

http --header http://httpbin.org/get

http --headers http://httpbin.org/get

only body

http -b http://httpbin.org/get

http --body http://httpbin.org/get

http https://api.github.com/search/repositories q==httpie per_page==1

download

http -d http://httpbin.org/get -o get

http http://httpbin.org/get > get

cat get

different methods

` http -f POST http://httpbin.org/post username=’zgx’ `

http DELETE http://httpbin.org/delete

http PUT http://httpbin.org/put username='zgx' no=11

` http PUT http://httpbin.org/put username=’zgx’ no:=1 update:=true list:=’[“a”, “b”]’`

模拟Form的Post请求, Content-Type: application/x-www-form-urlencoded; charset=utf-8 http --form POST http://httpbin.org/post username='zgx''

模拟Form的上传, Content-Type: multipart/form-data http -f POST http://httpbin.org/post username='zgx' file@txt_file

这里cookie使用单引号括起来,是因为包含多个cooke.如果是单个cookie可以去掉单引号.

http http://httpbin.org/get User-Agent:curl 'Cookie:a=b;b=c' Referer:http://blog.zgx.io

user-defined boundary

http --multipart --boundary=asdfghjkl --offline example.org hello=world

auto-defined

http --form --multipart --offline example.org hello=world Content-Type:multipart/letter

localhost port 80

http :/api/v1/post/get cursor==0 limit==20 desc==1 tag==5

path-as-is

http --path-as-is -v example.org/./../../etc/password

GET /../../etc/password HTTP/1.1

http -v example.org/./../../etc/password

GET /etc/password HTTP/1.1

转义字符

http POST http://httpbin.org/post -- -name-starting-with-dash=foo -Unusual-Header:bar

Json

http PUT http://httpbin.org/put \
name=John \
age:=20 \
xx:=true \
hobbies:='["http", "pies"]' \
bookmarks:=@data.json \
description=@text.txt

echo '{"key" : "value"}' | http POST http://httpbin.org/post

Sessions

http --session=logged-in -a username:password pie.dev/get API-Key:123

http --session=logged-in pie.dev/headers

鉴权[pending]

http -a username:password http://httpbin.org/basic-auth/username/password

代理

e.g.

http --proxy=http:http://217.107.197.174:8081 proxy.mimvp.com

offline

生成HTTP request,但不发送

http --offline POST server.chess/api/games API-Key:ZZZ w=magnus b=hikaru t=180 i=2

重定向到文件,后续发送

http --offline POST server.chess/api/games API-Key:ZZZ w=magnus b=hikaru t=180 i=2 > http.request

nc -v pie.dev 80 < request.http

redirects

http --follow --all zhihu.com -h

http --follow --all zhihu.com --max-redirects=2

源码

rst

Makefile


.
├── AUTHORS.rst
├── CHANGELOG.rst
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.rst
├── extras
│   ├── brew-deps.py
│   ├── httpie-completion.bash
│   ├── httpie-completion.fish
│   └── httpie.rb
├── httpie
│   ├── cli
│   ├── client.py
│   ├── compat.py
│   ├── config.py
│   ├── context.py
│   ├── core.py
│   ├── downloads.py
│   ├── __init__.py
│   ├── __main__.py
│   ├── models.py
│   ├── output
│   ├── plugins
│   ├── __pycache__
│   ├── sessions.py
│   ├── ssl.py
│   ├── status.py
│   ├── uploads.py
│   └── utils.py
├── httpie.egg-info
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── httpie.gif
├── httpie.png
├── LICENSE
├── Makefile
├── MANIFEST.in
├── README.rst
├── requirements-dev.txt
├── setup.cfg
├── setup.py
├── tests
│   ├── client_certs
│   ├── conftest.py
│   ├── fixtures
│   ├── __pycache__
│   ├── README.rst
│   ├── test_auth_plugins.py
│   ├── test_auth.py
│   ├── test_binary.py
│   ├── test_cli.py
│   ├── test_compress.py
│   ├── test_config.py
│   ├── test_defaults.py
│   ├── test_docs.py
│   ├── test_downloads.py
│   ├── test_errors.py
│   ├── test_exit_status.py
│   ├── test_httpie.py
│   ├── test_offline.py
│   ├── test_output.py
│   ├── test_redirects.py
│   ├── test_regressions.py
│   ├── test_sessions.py
│   ├── test_ssl.py
│   ├── test_stream.py
│   ├── test_tokens.py
│   ├── test_unicode.py
│   ├── test_uploads.py
│   ├── test_windows.py
│   └── utils
└── venv
    ├── bin
    ├── include
    ├── lib
    ├── lib64 -> lib
    ├── pip-selfcheck.json
    └── pyvenv.cfg

17 directories, 64 files

醍醐灌顶

进制计算方法

这写的是相当的精辟,这思维可以适用很多场景.

owner也是直接搬运Doug Latornell这位老哥写的.

code-show

def humanize_bytes(n, precision=2):
    # Author: Doug Latornell
    # Licence: MIT
    # URL: https://code.activestate.com/recipes/577081/
    """Return a humanized string representation of a number of bytes.

    Assumes `from __future__ import division`.

    >>> humanize_bytes(1)
    '1 B'
    >>> humanize_bytes(1024, precision=1)
    '1.0 kB'
    >>> humanize_bytes(1024 * 123, precision=1)
    '123.0 kB'
    >>> humanize_bytes(1024 * 12342, precision=1)
    '12.1 MB'
    >>> humanize_bytes(1024 * 12342, precision=2)
    '12.05 MB'
    >>> humanize_bytes(1024 * 1234, precision=2)
    '1.21 MB'
    >>> humanize_bytes(1024 * 1234 * 1111, precision=2)
    '1.31 GB'
    >>> humanize_bytes(1024 * 1234 * 1111, precision=1)
    '1.3 GB'

    """
    abbrevs = [
        (1 << 50, 'PB'),
        (1 << 40, 'TB'),
        (1 << 30, 'GB'),
        (1 << 20, 'MB'),
        (1 << 10, 'kB'),
        (1, 'B')
    ]

    if n == 1:
        return '1 B'

    for factor, suffix in abbrevs:
        if n >= factor:
            break

    # noinspection PyUnboundLocalVariable
    return '%.*f %s' % (precision, n / factor, suffix)

衍生

其实可以参考该项目源代码来进行爬虫实现

参考

Github-doc

HTTPie-doc

https://www.cnblogs.com/new_2050/p/7745788.html

https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data


Similar Posts

下一篇 Minio

Comments