<small id='QxIoAZt'></small> <noframes id='7ftSzYHdRy'>

  • <tfoot id='rEswOV'></tfoot>

      <legend id='8yL3Jlr'><style id='VKxy6'><dir id='8lZVIY'><q id='C8XGZ'></q></dir></style></legend>
      <i id='DE4JB21C'><tr id='fhIEK'><dt id='H5bw1VtPns'><q id='QZA2'><span id='1Xfd'><b id='IhpHgyfb06'><form id='gk9MTNYvPa'><ins id='P4zAZvO'></ins><ul id='GNPVOX6'></ul><sub id='9YlsVxT1'></sub></form><legend id='G0iJv'></legend><bdo id='CGDzcs'><pre id='CDAWPfLb4'><center id='YTWwe6z4as'></center></pre></bdo></b><th id='CWQ0L3'></th></span></q></dt></tr></i><div id='t4XM'><tfoot id='6oKb97QLz2'></tfoot><dl id='io1Oj'><fieldset id='0miD1AXs'></fieldset></dl></div>

          <bdo id='Geok0R'></bdo><ul id='gqwNa'></ul>

          1. <li id='h0xVfcNm'></li>
            登陆

            章鱼彩票网-运用 HTTPie 进行 API 测验

            admin 2019-11-01 127人围观 ,发现0个评论

            运用 HTTPie 调试 API,这是一个用 Python 写的易用的指令行东西。

            -- Moshe Zadka(作者)

            HTTPie 是一个十分易用、易于晋级的 HTTP 客户端。它的发音为 “aitch-tee-tee-pie” 并以 http 指令运转,它是一个用 Python 编写的来用于拜访 Web 的指令行东西。

            由于这是一篇关于 HTTP 客户端的辅导文章,因而你需求一个 HTTP 服务器来试用它。在这里,拜访 httpbin.org ,它是一个简略的开源 HTTP 恳求和呼应服务。httpbin.org 网站是一种测验 Web API 的强壮办法,并能细心办理并显现恳求和呼应内容,不过现在韩国化妆品品牌让我们专心于 HTTPie 的强壮功用。

            Wget 和 cURL 的替代品

            你或许听说过陈旧的 Wget 或略微新一些的 cURL 东西,它们答应你从指令行拜访 Web。它们是为拜访网站而编写的,而 HTTPie 则用于拜访 Web API。

            网站恳求发生在计算机和正在阅览并呼应它所看到的内容的终究用户之间,这并不太依赖于结构化的呼应。可是,API 恳求会在两台计算机之间进行结构化调用,人并不是该流程内的一部分,像 HTTPie 这样的指令行东西的参数能够有效地处理这个问题。

            装置 HTTPie

            有几种办法能够装置 HTTPie。你能够经过包办理器装置,不管你运用的是 brew、apt、yum 仍是 dnf。可是,假如你已装备 virtualenvwrapper ,那么你能够用自己的办法装置:

            $ mkvirtualenv httpie
            ...
            (httpie) $ pip install httpie
            ...
            (httpie) $ deactivate
            $ alias http=~/.virtualenvs/httpie/bin/http
            $ http -b GET https://httpbin.org/get
            {
            "args": {},
            "headers": {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Host": "httpbin.org",
            "User-Agent": "HTTPie/1.0.2"
            },
            "origin": "104.220.242.210, 104.220.242.210",
            "url": "https://httpbin.org/get"
            }

            经过将 http 别号指向为虚拟环境中的指令,即便虚拟环境在非活动状况,你也能够运转它。你能够将 alias 指令放在 .bash_profile 或 .bashrc 中,这样你就能够运用以下指令晋级 HTTPie:

            $ ~/.virtualenvs/httpie/bin/pip install -U pip

            运用 HTTPie 查询网站

            HTTPie 能够简化查询和测验 API。上面运用了一个选项,-b(即 --body)。没有它,HTTPie 将默许打印整个呼应,包含呼应头:

            $ http GET https://httpbin.org/get
            HTTP/1.1 200 OK
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Connection: keep-alive
            Content-Encoding: gzip
            Content-Length: 177
            Content-Type: application/json
            Date: Fri, 09 Aug 2019 20:19:47 GMT
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            {
            "args": {},
            "headers": {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Host": "httpbin.org",
            "User-Agent": "HTTPie/1.0.2"
            },
            "origin": "104.220.242.210, 104.220.242.210",
            "url": "https://httpbin.org/get"
            }

            这在调试 API 服务时十分重要,由于很多信息在呼应头中发送。例如,检查发送的 cookie 一般很重要。httpbin.org 供给了经过 URL 途径设置 cookie(用于测验意图)的办法。以下设置一个标题为 opensource, 值为 awesome 的 cookie:

            $ http GET https://httpbin.org/cookies/set/opensource/awesome
            HTTP/1.1 302 FOUND
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Connection: keep-alive
            Content-Length: 223
            Content-Type: text/html; charset=utf-8
            Date: Fri, 09 Aug 2019 20:22:39 GMT
            Location: /cookies
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            Set-Cookie: opensource=awesome; Path=/
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block

            Redirecting...

            Redirecting...


            You should be redirected automatically to target URL:
            /cookies. If not click the link.

            留意 Set-Cookie: opensource=awesome; Path=/ 的呼应头。这表明你预期设置的 cookie 已正确设置,途径为 /。另请留意,即便你得到了 302 重定向,http 也不会遵从它。假如你想要遵从重定向,则需求清晰运用 --follow 标志恳求:

            $ http --follow GET https://httpbin.org/cookies/set/opensource/awesome
            HTTP/1.1 200 OK
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Connection: keep-alive
            Content-Encoding: gzip
            Content-Length: 66
            Content-Type: application/json章鱼彩票网-运用 HTTPie 进行 API 测验
            Date: Sat, 10 Aug 2019 01:33:34 GMT
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            {
            "cookies": {
            "opensource": "awesome"
            }
            }

            但此刻你无法看到本来的 Set-Cookie 头。为了看到中心呼应,你需求运用 --all:

            $ http --h章鱼彩票网-运用 HTTPie 进行 API 测验eaders --all --follow GET https://httpbin.org/cookies/set/opensource/awesome
            HTTP/1.1 302 FOUND
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Content-Type: text/html; charset=utf-8
            Date: Sat, 10 Aug 2019 01:38:40 GMT
            Location: /cookies
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            Set-Cookie: opensource=awesome; Path=/
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            Content-Length: 223
            Connection: keep-alive
            HTTP/1.1 200 OK
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Content-Encoding: gzip
            Content-Type: application/json
            Date: Sat, 10 Aug 2019 01:38:41 GMT
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            Content-Length: 66
            Connection: keep-alive

            打印呼应体并不风趣,由于你大多数时分只关怀 cookie。假如你想看到中心恳求的呼应头,而不是终究恳求中的呼应体,你能够运用:

            $ http --pr章鱼彩票网-运用 HTTPie 进行 API 测验int hb --history-print h --all --follow GET https://httpbin.org/cookies/set/opensource/awesome
            HTTP/1.1 302 FOUND
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Content-Type: text/html; charset=utf-8
            Date: Sat, 10 Aug 2019 01:40:56 GMT
            Location: /cookies
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            Set-Cookie: opensource=awesome; Path=/
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            Content-Length: 223
            Connection: keep-alive
            HTTP/1.1 200 OK
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Content-Encoding: gzip
            Content-Type: application/json
            Date: Sat, 10 Aug 2019 01:40:56 GMT
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            Content-Length: 66
            Connection: keep-alive
            {
            "cookies": {
            "opensource": "awesome"
            }
            }

            你能够运用 --print 准确操控打印的内容(h:呼应头;b:呼应体),并运用 --history-print 掩盖中心恳求的打印内容设置。

            运用 HTTPie 下载二进制文件

            有时呼应体并不是文本方式,它需求发送到可被不同运用翻开的文件:

            $ http GET https://httpbin.org/image/jpeg
            HTTP/1.1 200 OK
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Connection: keep-alive
            Content-Length: 35588
            Content-Type: image/jpeg
            Date: Fri, 09 Aug 2019 20:25:49 GMT
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            +-----------------------------------------+
            | NOTE: binary data not shown in terminal |
            +-----------------------------------------+

            要得到正确的图片,你需求保存到文件:

            $ http --download GET https://httpbin.org/image/jpeg
            HTTP/1.1 200 OK
            Access-Control-Allow-Credentials: true
            Access-Control-Allow-Origin: *
            Connection: keep-alive
            Content-Length: 35588
            Content-Type: image/jpeg
            Date: Fri, 09 Aug 2019 20:28:13 GMT
            Referrer-Policy: no-referrer-when-downgrade
            Server: nginx
            X-Content-Type-Options: nosniff
            X-Frame-Options: DENY
            X-XSS-Protection: 1; mode=block
            Downloading 34.75 kB to "jpeg.jpe"
            Done. 34.75 kB in 0.00068s (50.05 MB/s)

            试一下!图片很心爱。

            运用 HTTPie 发送自定义恳求

            你能够发送指定的恳求头。这关于需求非标准头的自定义 Web API 很有用:

            $ http GET https://httpbin.org/headers X-Open-Source-Com:Awesome
            {
            "headers": {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Host": "httpbin.org",
            "User-Agent": "HTTPie/1.0.2",
            "X-Open-Source-Com": "Awesome"
            }
            }

            最终,假如要发送 JSON 字段(虽然能够指定切当的内容),关于许多嵌套较少的输入,你能够运用快捷办法:

            $ http --body PUT https://httpbin.org/anything open-source=awesome author=moshez
            {
            "args": {},
            "data": "{\"open-sourc章鱼彩票网-运用 HTTPie 进行 API 测验e\": \"awesome\", \"author\": \"moshez\"}",
            "files": {},
            "form": {},
            "headers": {
            "Accept": "application/json, */*",
            "Accept-Encoding": "gzip, deflate",
            "Content-Length": "46",
            "Content-Type": "application/json",
            "Host": "httpbin.org",
            "User-Agent": "HTTPie/1.0.2"
            },
            "json": {
            "author": "moshez",
            "open-source": "awesome"
            },
            "method": "PUT",
            "origin": "73.162.254.113, 73.162.254.113",
            "url": "https://httpbin.org/anything"
            }

            下次在调试 Web API 时,不管是你自己的仍是他人的,记住放下 cURL,试试 HTTPie 这个指令行东西。


            via: https://opensource.com/article/19/8/getting-started-httpie

            作者: Moshe Zadka 选题: lujun9972 译者: geekpi 校正: wxy

            本文由 LCTT 原创编译, Linux我国 荣誉推出

            点击“了解更多”可拜访文内链接

            请关注微信公众号
            微信二维码
            不容错过
            Powered By Z-BlogPHP