1个不稳定版本
0.1.0 | 2020年1月21日 |
---|
#421 在 HTTP客户端
90KB
2K SLoC
dot-http
dot-http是一个基于文本的脚本化HTTP客户端。它是一种类似于实际HTTP协议的简单语言,但仅添加了一些魔法,使其更实用,适用于构建和测试API的人。
安装
脚本
在命令提示符中输入以下内容
curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git bayne/dot-http
二进制发布
对于大多数用户来说,最简单的方法是下载预构建的二进制文件。您可以在发布页面上找到各种平台的二进制文件。
Cargo
首先,安装cargo。然后
$ cargo install dot-http
您需要使用稳定版本才能使其正常工作;如果有疑问,请运行
rustup run stable cargo install dot-http
用法
有关用法,请参阅dot-http --help
Vim
请参阅此插件以在Vim中使用dot-http。
请求
请求格式旨在尽可能接近HTTP。HTTP最初被设计成易于阅读和简单,为什么不这样做呢?
simple.http
GET http://httpbin.org
Accept: */*
执行该脚本会将响应打印到stdout
$ dot-http simple.http
GET http://httpbin.org/get
HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
content-type: application/json
date: Sat, 18 Jan 2020 20:48:50 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: 170
connection: keep-alive
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org"
},
"url": "https://httpbin.org/get"
}
变量
使用变量动态构建脚本,可以从环境文件或之前请求的响应处理程序中提取数据。
simple_with_variables.http
POST http://httpbin.org/post
Accept: */*
X-Auth-Token: {{token}}
{
"id": {{env_id}}
}
http-client.env.json
{
"dev": {
"env_id": 42,
"token": "SuperSecretToken"
}
}
请注意,变量将被其值替换
$ dot-http simple_with_variables.http
POST http://httpbin.org/post
HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
content-type: application/json
date: Sat, 18 Jan 2020 20:55:24 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: 342
connection: keep-alive
{
"args": {},
"data": "{\r\n \"id\": 42\r\n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "18",
"Host": "httpbin.org",
"X-Auth-Token": "SuperSecretToken"
},
"json": {
"id": 42
},
"url": "https://httpbin.org/post"
}
环境文件
使用环境文件来控制变量初始值的设置
http-client.env.json
{
"dev": {
"host": localhost,
"token": "SuperSecretToken"
},
"prod": {
"host": example.com,
"token": "ProductionToken"
}
}
env_demo.http
GET http://{{host}}
X-Auth-Token: {{token}}
指定不同的环境时调用命令会导致脚本中变量的值不同
$ dot-http -e dev env_demo.http
GET https://127.0.0.1
X-Auth-Token: SuperSecretToken
$ dot-http -e prod env_demo.htp
GET http://example.com
X-Auth-Token: ProductionToken
响应处理程序
使用之前的请求来填充未来请求中的一些数据
response_handler.http
POST http://httpbin.org/post
Content-Type: application/json
{
"token": "sometoken",
"id": 237
}
> {%
client.global.set('auth_token', response.body.json.token);
client.global.set('some_id', response.body.json.id);
%}
###
PUT http://httpbin.org/put
X-Auth-Token: {{auth_token}}
{
"id": {{some_id}}
}
来自之前请求的数据
$ dot-http test.http
POST http://httpbin.org/post
HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
content-type: application/json
date: Sat, 18 Jan 2020 21:01:59 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: 404
connection: keep-alive
{
"args": {},
"data": "{\r\n \"token\": \"sometoken\",\r\n \"id\": 237\r\n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "46",
"Content-Type": "application/json",
"Host": "httpbin.org"
},
"json": {
"id": 237,
"token": "sometoken"
},
"url": "https://httpbin.org/post"
}
可以在未来的请求中填充数据
$ dot-http -l 16 test.http
PUT http://httpbin.org/put
HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
content-type: application/json
date: Sat, 18 Jan 2020 21:02:28 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: 336
connection: keep-alive
{
"args": {},
"data": "{\r\n \"id\": 237\r\n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "19",
"Host": "httpbin.org",
"X-Auth-Token": "sometoken"
},
"json": {
"id": 237
},
"url": "https://httpbin.org/put"
}
贡献
欢迎贡献和建议!
在提交PR之前,请创建一个问题,只有引用了现有问题的PR才会被接受。如果您有建议的更改,请首先创建一个问题,以便我们可以讨论。
许可证
依赖项
~16–21MB
~405K SLoC