1, 用Net::HTTP.get
uri = URI('http://xxxxxxx.com/api/user/1.0/json/get_info?&user_id=3')
Net::HTTP.get(uri)
2, 用Net::HTTP.get 时加一些参数
uri = URI('http://xxxxxx.com/api/user/1.0/json/get_info')
params = { :user_id => 3}
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
3, Net::HTTP.post_form
uri = URI('http://xxxxx.com/api/user/1.0/json/login')
res = Net::HTTP.post_form(uri, 'client_id' => '159d8a2f931514c107280', 'identity' => 'xxxxx002','password' => 'xxxxxx')
puts res.body
4, res.methods里面有些很有用的方法
uri = URI('http://drhel31.sh.magima.com/api/user/1.0/json/get_info?&user_id=3')
res = Net::HTTP.get_response(uri)
# Headers
res.header # => "#<Net::HTTPOK 200 OK readbody=true>"
res.read_header # => "#<Net::HTTPOK 200 OK readbody=true>"
# Status 这个很有用,做接口调用成功与否的判断可用。
puts res.code # => '200'
puts res.message # => 'OK'
puts res.class.name # => 'HTTPOK'
res.code_type # => Net::HTTPOK
res.inspect # => "#<Net::HTTPOK 200 OK readbody=true>"
# Body
puts res.body if res.msg == "OK"
5, 判断文件是否存在
File.exist? '/home/test/features/support/modul_login.rb'
转载于:https://blog.51cto.com/rubyisteleanor/1403013