2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

HTTPie で GraphQL を使う

Last updated at Posted at 2022-07-25

https://countries.trevorblades.com/
にアクセスしてみます。

日本

get_jp.sh
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"
        }
    }
}

韓国

get_kr.sh
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"
        }
    }
}

ウクライナ

get_ua.sh
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_jp.json
{"query": "query {country (code: \"JP\") {name native currency phone }}"}
get_jp_a.sh
URL="https://countries.trevorblades.com/"
#
http $URL < query_jp.json
get_jp_b.sh
URL="https://countries.trevorblades.com/"
#
http $URL @query_jp.json

確認したバージョン

$ http --version
3.2.1
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?