[Ruby]Rubyでcurlするときのgemのcurbの使い方
curbの使い方
curbとはrubyのコード内でcurlコマンドを使えるようにするgemです。github.comなかなか日本語ではドキュメントがないので、APIに接続する際のコードを例にあげておきます。
GET
require 'curb' endpoint = 'https://hogehoge.com' # change for yourself action = '/get_information' # change for yourself res = Curl.get("#{endpoint}#{action}") do |res| res.enable_cookies = true res.cookiefile = File.join(File.dirname(__FILE__), "cookies") res.cookiejar = File.join(File.dirname(__FILE__), "cookies") end return res.body_str
POST
Ex.) ログインなどでユーザ情報と共におくる場合などrequire 'curb' endpoint = 'https://hogehoge.com' # change for yourself action = '/auth' # change for yourself res = Curl.post("#{endpoint}#{action}", user_info) do |res| res.enable_cookies = true res.cookiejar = File.join(File.dirname(__FILE__), "cookies") end return res.body_str