Ruby学习笔记系列(一)

发布时间:2020-06-21 21:20:06 作者:夜半煮饭
来源:网络 阅读:367

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'



推荐阅读:
  1. Vue由浅入深学习系列笔记之01
  2. jQuery 学习系列笔记(三)(续)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ruby (一)

上一篇:如何配置Apache让CGI执行

下一篇:CSS中的外边距属性

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》