当前位置: 首页 > 工具软件 > HTTPie > 使用案例 >

httpie_HTTPie入门进行API测试

鲁浩言
2023-12-01

httpie

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

两台计算机之间的结构化调用。 人员不是图片的一部分,HTTPie等命令行工具的参数可以有效地处理此问题。

安装HTTPie

有几种安装HTTPie的方法。 无论使用brewaptyum还是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 httpie 

使用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的cookie设置为awesome值:


   
   
$ 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

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN" >
< title > Redirecting... </ title >
< h1 > Redirecting... </ h1 >
< p > You should be redirected automatically to target URL:
< a href = "/cookies" >/ cookies </ a > .  If not click the link.

注意Set-Cookie:opensource = awesome; 路径= /标头。 这表明您期望设置的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
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 --headers --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 --print 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精确控制要打印的内容,并使用--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-source \" : \" 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(无论是您自己的还是其他人的Web API)时,请放下cURL并访问HTTPie(Web API的命令行客户端)。

翻译自: https://opensource.com/article/19/8/getting-started-httpie

httpie

 类似资料: