Rubyã§ããã·ã¥ããã©ã¡ã¼ã¿ã«ãã¦HTTPãªã¯ã¨ã¹ããéä¿¡ããã
以ä¸ã®3ã¤ã®ãã¨ãã¾ã¨ãã¦å®è¡ããã¡ã½ãããæ¸ããã
- Hashããã¯ã¨ãªã¼æååä½æã
- URLã®ãã¼ã¹ã
- Basicèªè¨¼ã
response = http_request("get", "http://www.example.com/search", {:id => 123, :name => "koseki"}, "user", "pass")
ã¿ããã«ãã¦ä½¿ãã
require 'net/http' require 'uri' def http_request(method, uri, query_hash = {}, user = nil, pass = nil) uri = URI.parse(uri) if uri.is_a? String method = method.to_s.strip.downcase query_string = (query_hash||{}).map{|k,v| URI.encode(k.to_s) + "=" + URI.encode(v.to_s) }.join("&") if method == "post" args = [Net::HTTP::Post.new(uri.path), query_string] else args = [Net::HTTP::Get.new(uri.path + (query_string.empty? ? "" : "?#{query_string}"))] end args[0].basic_auth(user, pass) if user Net::HTTP.start(uri.host, uri.port) do |http| return http.request(*args) end end
ãªãã¤ã¬ã¯ãã«å¯¾å¿ããhttp_request2ãgistã«ãããã
以ä¸ãåç §ã
- Addressable è¤éãªãã©ã¡ã¼ã¿ãã¯ã¨ãªã¼æååã«ã
- 404 Not Found HttpClient