使用HTTPie来调用GraphQL
我将尝试访问”https://countries.trevorblades.com/”。
东亚国家。
URL="https://countries.trevorblades.com/"
#
http $URL query='query {country (code: "JP") {name native currency phone }}'
运行结果
$ ./get_jp.sh
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 103
accept-ranges: bytes
access-control-allow-credentials: true
access-control-allow-headers: *
access-control-allow-methods: POST, GET, HEAD, OPTIONS
access-control-allow-origin: *
access-control-expose-headers: *
access-control-max-age: 600
age: 275
cache-control: public, s-maxage=2628000, stale-while-revalidate=2628000
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Sat, 15 Apr 2023 00:34:21 GMT
gcdn-cache: HIT
vary: Accept-Encoding
via: 1.1 varnish
x-cache: HIT
x-cache-hits: 2
x-powered-by: Stellate
x-served-by: cache-hnd18738-HND
x-timer: S1681518861.280412,VS0,VE0
{
"data": {
"country": {
"currency": "JPY",
"name": "Japan",
"native": "日本",
"phone": "81"
}
}
}
朝鲜
URL="https://countries.trevorblades.com/"
#
http $URL query='query {country (code: "KR") {name native currency phone }}'
运行结果
$ ./get_kr.sh
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 118
accept-ranges: bytes
access-control-allow-credentials: true
access-control-allow-headers: *
access-control-allow-methods: POST, GET, HEAD, OPTIONS
access-control-allow-origin: *
access-control-expose-headers: *
access-control-max-age: 600
age: 205
cache-control: public, s-maxage=2628000, stale-while-revalidate=2628000
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Sat, 15 Apr 2023 00:35:23 GMT
gcdn-cache: HIT
vary: Accept-Encoding
via: 1.1 varnish
x-cache: HIT
x-cache-hits: 1
x-powered-by: Stellate
x-served-by: cache-hnd18742-HND
x-timer: S1681518923.446620,VS0,VE1
{
"data": {
"country": {
"currency": "KRW",
"name": "South Korea",
"native": "대한민국",
"phone": "82"
}
}
}
乌克兰
URL="https://countries.trevorblades.com/"
#
http $URL query='query {country (code: "UA") {name native currency phone }}'
执行结果
$ ./get_ua.sh
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 115
accept-ranges: bytes
access-control-allow-credentials: true
access-control-allow-headers: *
access-control-allow-methods: POST, GET, HEAD, OPTIONS
access-control-allow-origin: *
access-control-expose-headers: *
access-control-max-age: 600
age: 156
cache-control: public, s-maxage=2628000, stale-while-revalidate=2628000
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Sat, 15 Apr 2023 00:35:53 GMT
gcdn-cache: HIT
vary: Accept-Encoding
via: 1.1 varnish
x-cache: HIT
x-cache-hits: 1
x-powered-by: Stellate
x-served-by: cache-hnd18747-HND
x-timer: S1681518953.456675,VS0,VE1
{
"data": {
"country": {
"currency": "UAH",
"name": "Ukraine",
"native": "Україна",
"phone": "380"
}
}
}
将查询内容保存到另一个文件中。
{"query": "query {country (code: \"JP\") {name native currency phone }}"}
URL="https://countries.trevorblades.com/"
#
http $URL < query_jp.json
URL="https://countries.trevorblades.com/"
#
http $URL @query_jp.json
确认的版本
$ http --version
3.2.1