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'
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。