hj
hj is a command line tool to convert HTTP/1-style text into JSON.
This command is inspired by yusukebe/rj, which is a standalone HTTP client that shows HTTP responses in JSON. On the other hand, this command is not an HTTP client but a simple text filter that translates a result of an external HTTP client such as curl(1).
Synopsis
Simple usage with curl(1):
# -sv is required to output HTTP response headers.
$ curl -sv https://httpbin.org/get 2>&1 | hj | jq .
The output may be:
Example Domain
\nThis domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.
\n \n{
"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": "\n\n\n Example Domain \n\n
\n
\n
\n \n\n\n\n
\n
Example Domain
\n
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.
\n
\n
\n\n\n"
}
If the response is JSON (content-type: application/json
), its content is automatically parsed as JSON:
curl -sv https://jsonplaceholder.typicode.com/todos/1 2>&1 | hj | jq .
output:
{
"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
}
}
Install
cargo(1) and rustc(1) (>= v1.58) are required to install:
cargo install --path .
Supported Commands
- curl - https://curl.se/
-sv
options are required forhj
- h2o-httplicent - https://github.com/h2o/h2o/
- Any of HTTP/1 style texts
License
Copyright 2022 FUJI Goro.
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.