公式ドキュメントを読むだけではわからず、結局ソースを読んで調べたのでメモ。単純なファイルアップロードはサンプルがあったのですが、パラメタ付きで送る方法はサンプルとは違う方法しかないっぽい。
Rubyのクライアントはいろいろあるみたいだけど、今回はrest-client
を利用。
https://github.com/rest-client/rest-client
基本的な使い方はこんな感じ。RestClient.get
やRestClient.post
のように簡単にリクエスト送信できる。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=example1
payload
(日本語で貨物とか搭載量とか。ここではデータ本体という意味らしい)にいろいろ突っ込んで使うのが基本。
よくある使い方例。まずはシンプルにget
でurl
を叩く。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=get
クエリパラメタを付与する場合。以下の例だとURLの末尾に?accept=json
がつく。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=query_parameter
パラメタはネストも可能。payload
を以下のように書くと・・・
:user => {:name => "daipresents"}
以下のようなフォームにdaipresents
が入力されているという意味になる。
<input name="user[name]" value="daipresents" />
post
も簡単。以下はjson
を詰め込んで送る例。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=post
問題はファイルをアップロードしたい場合。ドキュメントにはこの例が書かれている。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=multipart1
こう書くと:transfer
と:upload
の2ファイルをアップロードするという意味になる。試してないけどファイルはファイルオブジェクトでもパスでもいいみたいね。
multipart
で送るケースは以下の感じ。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=multipart2
ファイルがないけどmultipart
で送りたいときは、以下のようにmultipart
パラメタをつけてあげる。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=multipart3
Basic認証情報をつける場合は以下。参考したのは「Ruby rest-client file upload as multipart form data with basic authenticaion」
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=multipart4
問題は、ファイルアップロードするときにクエリパラメタをつける場合。多分、このケースだとRestClient.post
メソッドは使えない。
例えば、上のようにmyfile
という名前で、image.jpg
ファイルを送る場合、リクエストに以下のようなフィールドが必要になる。
Content-Disposition: form-data name="my file" filename="image.jpg"
たとえば、こんな感じで書いてみると
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=multipart5
Content-Disposition
フィールドはこうなる。
Content-Disposition: form-data; name="name" daipresents
payload.rb
がこの辺を頑張ってくれるんだけど、パラメタ全部をファイル情報としか扱ってくれない実装に見える。
となると、URL部分にパラメタを自分でつけるしかなさそうだけど、「[RoR]rest-clientでpaperclipに対応した画像フォーム送信を再現してみる」を読んでいてRestClient::Requestならいけそうなことがわかった。
実際にできた例はこちら。
https://gist.github.com/daipresents/528891497cc6e89f5c06e597a8dcc2e1.js?file=multipart6
よかったよかった。
同じpayloadのはずなんだけど、正しい書き方があるのかもしれない。