1 个不稳定版本
0.1.0 | 2022 年 2 月 16 日 |
---|
#34 in #json-response
19KB
325 行代码(不含注释)
hj
hj 是一个命令行工具,用于将 HTTP/1-style 文本转换为 JSON。
这个命令灵感来源于 yusukebe/rj,这是一个独立的 HTTP 客户端,可以以 JSON 格式显示 HTTP 响应。另一方面,这个命令不是一个 HTTP 客户端,而是一个简单的文本过滤器,它将外部 HTTP 客户端(如 curl(1))的结果转换为 JSON。
概述
使用 curl(1) 的简单用法
# -sv is required to output HTTP response headers.
$ curl -sv https://httpbin.org/get 2>&1 | hj | jq .
输出可能是
{
"protocol": "HTTP/1.1",
"status_code": 200,
"headers": {
"accept-ranges": "bytes",
"age": "417384",
"cache-control": "max-age=604800",
"content-type": "text/html; charset=UTF-8",
"date": "Tue, 25 Jan 2022 04:28:00 GMT",
"etag": "\"3147526947\"",
"expires": "Tue, 01 Feb 2022 04:28:00 GMT",
"last-modified": "Thu, 17 Oct 2019 07:18:26 GMT",
"server": "ECS (sab/56BC)",
"vary": "Accept-Encoding",
"x-cache": "HIT",
"content-length": "1256"
},
"content": "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"
}
如果响应是 JSON(content-type: application/json
),则其内容将自动解析为 JSON
curl -sv https://jsonplaceholder.typicode.com/todos/1 2>&1 | hj | jq .
输出
{
"protocol": "HTTP/1.1",
"status_code": 200,
"headers": {
"date": "Tue, 25 Jan 2022 04:57:52 GMT",
"content-type": "application/json; charset=utf-8",
"content-length": "83",
"connection": "keep-alive",
"x-powered-by": "Express",
"x-ratelimit-limit": "1000",
"x-ratelimit-remaining": "999",
"x-ratelimit-reset": "1631494143",
"vary": "Origin, Accept-Encoding",
"access-control-allow-credentials": "true",
"cache-control": "max-age=43200",
"pragma": "no-cache",
"expires": "-1",
"x-content-type-options": "nosniff",
"etag": "W/\"53-hfEnumeNh6YirfjyjaujcOPPT+s\"",
"via": "1.1 vegur",
"cf-cache-status": "HIT",
"age": "13133",
"accept-ranges": "bytes",
"expect-ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
"report-to": "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=kfDw3zHT7KgD4%2FmRBWFv5gYnwzVVSqFH5N%2F9sTmdI425jb9mZyZggJNoeaYF2%2B%2FdEQ57JdVJggqDJZSRKt5YX%2BStzgoYRGmSwcsQ5M%2Bd1vD9rzT72hrwQfh62ZXPa01QSJxhEpyJUmf7y8BVuagT\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
"nel": "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}",
"server": "cloudflare",
"cf-ray": "6d2eec5508f10ac0-NRT",
"alt-svc": "h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"
},
"content": {
"completed": false,
"userId": 1,
"title": "delectus aut autem",
"id": 1
}
}
安装
安装需要 cargo(1) 和 rustc(1) (≥ v1.58)
cargo install --path .
支持的命令
- curl - https://curl.se/
-sv
选项是hj
所必需的
- h2o-httplicent - https://github.com/h2o/h2o/
- 任何 HTTP/1 风格的文本
许可协议
版权所有 2022 FUJI Goro。
在此授予使用、复制、修改和/或出于任何目的分发此软件的许可,无论是否有费,前提是上述版权声明和本许可声明出现在所有副本中。
本软件按“原样”提供,作者放弃与此软件有关的全部保证,包括所有暗示的适销性和适用性保证。在任何情况下,作者均不对任何特殊、直接、间接或后果性损害或任何损害负责,无论这些损害是否由于合同、疏忽或其他侵权行为造成,无论是否与使用或性能有关。
依赖项
~7MB
~128K SLoC