|
| 1 | +--- |
| 2 | +title: Use as HTTP client |
| 3 | +--- |
| 4 | + |
| 5 | +:::note |
| 6 | +- **Prerequisite**: First, [setup **CHKware**](/setup) to continue |
| 7 | +- Find [More `http` examples](/examples/http-examples) here |
| 8 | +::: |
| 9 | + |
| 10 | +Let's call the [_XKCD.com JSON API_](https://xkcd.com/json.html) that returns a joke for given ID. Please do as follows: |
| 11 | + |
| 12 | +- Create a file called `xkcd-joke.chk` in any of your workspace |
| 13 | +- Open `xkcd-joke.chk` file, and add following spec. That is all you need to do a minimal HTTP GET request. See [Http example](/examples/http-examples) for other methods and types you can send with the requests. |
| 14 | + |
| 15 | + ```yaml |
| 16 | + --- |
| 17 | + version: default:http:0.7.2 |
| 18 | + request: |
| 19 | + url: https://xkcd.com/614/info.0.json |
| 20 | + method: GET |
| 21 | + ``` |
| 22 | +
|
| 23 | + Here notice the `verison` string **`version: default:http:0.7.2`**, this is important for this specification to be a [http specification](/references/http-reference). |
| 24 | + |
| 25 | +- Open a terminal. Hit enter after writing following command on terminal. |
| 26 | + |
| 27 | + ```bash |
| 28 | + chk http xkcd-joke.chk |
| 29 | + ``` |
| 30 | + |
| 31 | + You'll get output like following. Data will vary depending on the day you are doing it. |
| 32 | + |
| 33 | + ```bash |
| 34 | + File: xkcd-joke.chk |
| 35 | +
|
| 36 | + - Making request [Success] |
| 37 | + - Prepare exposable [Success] |
| 38 | +
|
| 39 | + --- |
| 40 | + HTTP/1.1 200 OK |
| 41 | +
|
| 42 | + Connection: keep-alive |
| 43 | + Content-Length: 660 |
| 44 | + Server: nginx |
| 45 | + Content-Type: application/json |
| 46 | + Last-Modified: Fri, 27 Jan 2023 12:54:53 GMT |
| 47 | + ETag: W/"63d3c99d-5ac" |
| 48 | + Expires: Sat, 28 Jan 2023 21:16:28 GMT |
| 49 | + Cache-Control: max-age=300 |
| 50 | + Content-Encoding: gzip |
| 51 | + Via: 1.1 varnish, 1.1 varnish |
| 52 | + Accept-Ranges: bytes |
| 53 | + Date: Sun, 29 Jan 2023 06:02:09 GMT |
| 54 | + Age: 0 |
| 55 | + X-Served-By: cache-dfw-kdfw8210071-DFW, cache-qpg1226-QPG |
| 56 | + X-Cache: HIT, HIT |
| 57 | + X-Cache-Hits: 1, 1 |
| 58 | + X-Timer: S1674972129.844278,VS0,VE286 |
| 59 | + Vary: Accept-Encoding |
| 60 | +
|
| 61 | + {"month": "7", "num": 614, "link": "", "year": "2009", "news": "", "safe_title": "Woodpecker", "transcript": "[[A man with a beret and a woman are standing on a boardwalk, leaning on a handrail.]]\nMan: A woodpecker!\n<<Pop pop pop>>\nWoman: Yup.\n\n[[The woodpecker is banging its head against a tree.]]\nWoman: He hatched about this time last year.\n<<Pop pop pop pop>>\n\n[[The woman walks away. The man is still standing at the handrail.]]\n\nMan: ... woodpecker?\nMan: It's your birthday!\n\nMan: Did you know?\n\nMan: Did... did nobody tell you?\n\n[[The man stands, looking.]]\n\n[[The man walks away.]]\n\n[[There is a tree.]]\n\n[[The man approaches the tree with a present in a box, tied up with ribbon.]]\n\n[[The man sets the present down at the base of the tree and looks up.]]\n\n[[The man walks away.]]\n\n[[The present is sitting at the bottom of the tree.]]\n\n[[The woodpecker looks down at the present.]]\n\n[[The woodpecker sits on the present.]]\n\n[[The woodpecker pulls on the ribbon tying the present closed.]]\n\n((full width panel))\n[[The woodpecker is flying, with an electric drill dangling from its feet, held by the cord.]]\n\n{{Title text: If you don't have an extension cord I can get that too. Because we're friends! Right?}}", "alt": "If you don't have an extension cord I can get that too. Because we're friends! Right?", "img": "https://imgs.xkcd.com/comics/woodpecker.png", "title": "Woodpecker", "day": "24"} |
| 62 | + ``` |
| 63 | + |
| 64 | + You just fetched a live API :rocket::star2:. |
| 65 | + |
| 66 | +- Now If you add `--result` or `-r` flag to the command then it should show you the result in formatted output. |
| 67 | + ```bash |
| 68 | + chk http xkcd-joke.chk --result |
| 69 | + ``` |
| 70 | + You should be able to see following if no exception occurs. |
| 71 | + |
| 72 | + ```bash |
| 73 | + HTTP/1.1 200 OK |
| 74 | +
|
| 75 | + Connection: keep-alive |
| 76 | + Content-Length: 660 |
| 77 | + Server: nginx |
| 78 | + Content-Type: application/json |
| 79 | + Last-Modified: Fri, 27 Jan 2023 12:54:53 GMT |
| 80 | + ETag: W/"63d3c99d-5ac" |
| 81 | + Expires: Sat, 28 Jan 2023 21:16:28 GMT |
| 82 | + Cache-Control: max-age=300 |
| 83 | + Content-Encoding: gzip |
| 84 | + Via: 1.1 varnish, 1.1 varnish |
| 85 | + Accept-Ranges: bytes |
| 86 | + Date: Sun, 29 Jan 2023 06:02:09 GMT |
| 87 | + Age: 0 |
| 88 | + X-Served-By: cache-dfw-kdfw8210071-DFW, cache-qpg1226-QPG |
| 89 | + X-Cache: HIT, HIT |
| 90 | + X-Cache-Hits: 1, 1 |
| 91 | + X-Timer: S1674972129.844278,VS0,VE286 |
| 92 | + Vary: Accept-Encoding |
| 93 | +
|
| 94 | + {"month": "7", "num": 614, "link": "", "year": "2009", "news": "", "safe_title": "Woodpecker", "transcript": "[[A man with a beret and a woman are standing on a boardwalk, leaning on a handrail.]]\nMan: A woodpecker!\n<<Pop pop pop>>\nWoman: Yup.\n\n[[The woodpecker is banging its head against a tree.]]\nWoman: He hatched about this time last year.\n<<Pop pop pop pop>>\n\n[[The woman walks away. The man is still standing at the handrail.]]\n\nMan: ... woodpecker?\nMan: It's your birthday!\n\nMan: Did you know?\n\nMan: Did... did nobody tell you?\n\n[[The man stands, looking.]]\n\n[[The man walks away.]]\n\n[[There is a tree.]]\n\n[[The man approaches the tree with a present in a box, tied up with ribbon.]]\n\n[[The man sets the present down at the base of the tree and looks up.]]\n\n[[The man walks away.]]\n\n[[The present is sitting at the bottom of the tree.]]\n\n[[The woodpecker looks down at the present.]]\n\n[[The woodpecker sits on the present.]]\n\n[[The woodpecker pulls on the ribbon tying the present closed.]]\n\n((full width panel))\n[[The woodpecker is flying, with an electric drill dangling from its feet, held by the cord.]]\n\n{{Title text: If you don't have an extension cord I can get that too. Because we're friends! Right?}}", "alt": "If you don't have an extension cord I can get that too. Because we're friends! Right?", "img": "https://imgs.xkcd.com/comics/woodpecker.png", "title": "Woodpecker", "day": "24"} |
| 95 | + ``` |
| 96 | + |
| 97 | +- These above response are coming as formatted output. If you want to see all JSON (headers, body, etc included) just pass the `--no-fomat` or `-nf` option flag. |
| 98 | + ```bash |
| 99 | + chk http xkcd-joke.chk --result --no-format |
| 100 | + ``` |
| 101 | + and response looks like this. |
| 102 | + |
| 103 | + ```bash |
| 104 | + [{"code": 200, "version": "HTTP/1.1", "reason": "OK", "headers": {"Connection": "keep-alive", "Content-Length": "660", "Server": "nginx", "Content-Type": "application/json", "Last-Modified": "Fri, 27 Jan 2023 12:54:53 GMT", "ETag": "W/\"63d3c99d-5ac\"", "Expires": "Sat, 28 Jan 2023 21:16:28 GMT", "Cache-Control": "max-age=300", "Content-Encoding": "gzip", "Via": "1.1 varnish, 1.1 varnish", "Accept-Ranges": "bytes", "Date": "Sun, 29 Jan 2023 06:13:14 GMT", "Age": "0", "X-Served-By": "cache-dfw-kdfw8210071-DFW, cache-qpg1267-QPG", "X-Cache": "HIT, HIT", "X-Cache-Hits": "1, 1", "X-Timer": "S1674972795.677591,VS0,VE316", "Vary": "Accept-Encoding"}, "body": {"month": "7", "num": 614, "link": "", "year": "2009", "news": "", "safe_title": "Woodpecker", "transcript": "[[A man with a beret and a woman are standing on a boardwalk, leaning on a handrail.]]\nMan: A woodpecker!\n<<Pop pop pop>>\nWoman: Yup.\n\n[[The woodpecker is banging its head against a tree.]]\nWoman: He hatched about this time last year.\n<<Pop pop pop pop>>\n\n[[The woman walks away. The man is still standing at the handrail.]]\n\nMan: ... woodpecker?\nMan: It's your birthday!\n\nMan: Did you know?\n\nMan: Did... did nobody tell you?\n\n[[The man stands, looking.]]\n\n[[The man walks away.]]\n\n[[There is a tree.]]\n\n[[The man approaches the tree with a present in a box, tied up with ribbon.]]\n\n[[The man sets the present down at the base of the tree and looks up.]]\n\n[[The man walks away.]]\n\n[[The present is sitting at the bottom of the tree.]]\n\n[[The woodpecker looks down at the present.]]\n\n[[The woodpecker sits on the present.]]\n\n[[The woodpecker pulls on the ribbon tying the present closed.]]\n\n((full width panel))\n[[The woodpecker is flying, with an electric drill dangling from its feet, held by the cord.]]\n\n{{Title text: If you don't have an extension cord I can get that too. Because we're friends! Right?}}", "alt": "If you don't have an extension cord I can get that too. Because we're friends! Right?", "img": "https://imgs.xkcd.com/comics/woodpecker.png", "title": "Woodpecker", "day": "24"}}] |
| 105 | + ``` |
| 106 | +--- |
| 107 | + |
| 108 | +This way you can use `chk http` as your script-able http client. Let's write a feature test or [testcase](/examples/testcase-examples) for this request. |
| 109 | + |
| 110 | +:wink::tada::confetti_ball: |
0 commit comments