CORDEA blog

Android applications engineer

Red で http request を行う方法

Red programming language で http request を行う方法がちょっと分かりづらかったのでメモ

GET

本当にシンプルな GET

>> read http://localhost:8080/api
== {{"status":"ok"}}

header の追加など必要な場合

>> write http://localhost:8080/api [GET [
    Accept: "application/json"
] ""]
== {{"status":"ok"}}

(読みやすいように実際の出力を一部修正してます)

POST

>> write http://localhost:8080/api [POST [
    Accept: "application/json"
] {{"data": "data"}}]
== {{"status":"ok"}}

Others

他は一緒です。
ただ、PATCH と DELETE は 0.6.3 時点で対応されていないのでご注意ください。

>> write http://localhost:8080/api [PUT [
    Accept: "application/json"
] ""]
== {{"status":"ok"}}
>> write http://localhost:8080/api [PATCH [
    Accept: "application/json"
] ""]
*** Internal Error: reserved for future use (or not yet implemented)
*** Where: write
*** Stack:  

>> write http://localhost:8080/api [DELETE [
    Accept: "application/json"
] ""]
*** Internal Error: reserved for future use (or not yet implemented)
*** Where: write
*** Stack:

refinement

read/write は refinement による挙動の違いがあります。

refinement の一覧はこの辺りで確認できます
read, write 共に binary, lines, info refinement が実装されてます
ちなみに write は append と part もエラー発生せずに通るのですが、みた感じ実装されてない気がします

binary

データを 16 進形式で返します

>> read/binary http://localhost:8080/api
== #{7B22737461747573223A226F6B227D}
lines

文字列を改行で区切って block で返します

>> read/lines http://localhost:8080/api
== ["text" "text" "text"]
info

名前の通り

>> info: read/info http://localhost:8080/api
== [200 #(
    X-Content-Type-Options: "nosniff"
    Server: "WEBrick...
>> print info
200 X-Content-Type-Options: "nosniff"
Server: "WEBrick/1.3.1 (Ruby/2.4.2/2017-09-14)"
Content-Length: "15"
Content-Type: "application/json"
Connection: "close"
Date: "Sun, 03 Dec 2017 09:47:41 GMT" {"status":"ok"}

詳しいことは実装見るのが一番早いので、simple-io あたりをご覧ください。