Update:It’s on GitHub now.
I always have to look up how to useNet::HTTP
, and I never find what I’m looking for. Behold, a cheat sheet!
A basic request
require "net/https"
require "uri"
uri = URI.parse("http://google.com")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({"User-Agent" => "My Ruby Script"})
response = http.request(request)
puts response.code
# => 301
puts response["location"] # All headers are lowercase
# => http://www.google.com/
URI
uri = URI.parse("http://mysite.com/some_api")
uri = URI.parse("https://mysite.com/thing?foo=bar")
# URI will also guess the correct port
URI.parse("http://foo.com").port # => 80
URI.parse("https://foo.com/").port # => 443
# Full reference
uri = URI.parse("http://foo.com/this/is/everything?query=params")
# p (uri.methods - Object.methods).sort
p uri.scheme # => "http"
p uri.host # => "foo.com"
p uri.port # => 80
p uri.request_uri # => "/this/is/everything?query=params"
p uri.path # => "/this/is/everything"
p uri.query # => "query=params"
# There are setters as well
uri.port = 8080
uri.host = "google.com"
uri.scheme = "ftp"
p uri.to_s
# => "ftp://google.com:8080/this/is/everything?query=param"
Everything else
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 3 # in seconds
http.read_timeout = 3 # in seconds
# The request.
request = Net::HTTP::Get.new(uri.request_uri)
# All the HTTP 1.1 methods are available.
Net::HTTP::Get
Net::HTTP::Post
Net::HTTP::Put
Net::HTTP::Delete
Net::HTTP::Head
Net::HTTP::Options
request.body = "Request body here."
request.initialize_http_header({"Accept" => "*/*"})
request.basic_auth("username", "password")
response = http.request(request)
response.body
response.status
response["header-here"] # All headers are lowercase
SSL
# Normal ssl
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# SSL .pem certs
pem = File.read("/path/to/my.pem")
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(pem)
http.key = OpenSSL::PKey::RSA.new(pem)
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
# Check for SSL dynamically. If your URI is https and you don't specify a
# port, the port will be 443, which is the correct SSL port.
http.use_ssl = (uri.port == 443)
分享到:
相关推荐
A cheatsheet for Ruby on Rails
Ruby元帅 Ruby YAML Java序列化 .NET序列化 SSTI / CSTI Flask / Jinja2 嫩枝/ Symfony 胸腺 AngularJS Vue.js Python 工具 SSRF 旁路 本地曝光 远程曝光 元数据 CRLF注射 指纹图 XXE 带外XXE 基于错误的...
Ruby OpenSSL速查表用例的集合,包括Ruby的OpenSSL绑定的示例。感到很自由在上发出拉取请求,或通过向我发送电子邮件。
pon-jp-cheat-sheet:庞的日语速查表
程序员或设计师能用上的91份速查表,包括各种语言和技术。包括Actionscript Apache Ant ASP C C# C++ Delphi Java Javascript Perl PHP Python VB Ruby Scala LISP Matlab Mathematica Fortran CSS DOM Erlang Perl ...
sketchup 二次开发的api类查询资料,可以快速检索sketchup 各个对象的类方法和属性,并且提供了示例代码,是进行sketchup二次开发的非常实用的...来源https://alexschreyer.net/sketchup/sketchup-ruby-api-cheatsheet/
异质Ruby记录所有Ruby专业知识。
编写一个包含tmux.cheatsheet单数据的文件(此处为tmux.cheatsheet ),例如: cheatsheet do title 'tmux cheatsheet' short_name 'tmux' # Used for the filename of the docset introduction 'My *awesome* ...
RubyMotion Bacon 规格备忘单贾蒙·霍尔姆格伦RubyMotion附带的内置叉 ,这本身就是一个叉,小纯Ruby的RSpec克隆。 RubyMotion 的 Bacon 功能相当强大,但文档记录并不完整。 这是一个备忘单,可帮助您记住用于测试 ...
将Ruby on Rail的基础知识汇集到一张纸上,超实用
RxSwift合并备忘单 这是一份对苹果新的框架感兴趣的开发人员的。 它基于以下博客文章: : RxSwift 结合 部署目标 iOS 8.0以上 iOS 13.0以上 支持平台 iOS,macOS,tvOS,watchOS,Linux 适用于Mac的iOS,macOS,...
.:: Reverse Shell Cheat Sheet Tool ::. .:: cyber-warrior.org ::.#安装注意克隆存储库: git clone 然后进入里面: cd shellver /然后安装: python setup.py -i 重新安装python setup.py -r 运行shellver -h或...
在控制台中docker-compose run --service-port --rm web以在调试模式下运行rails应用访问要调试的端点,以pry或byebug进入调试模式使用docker的Rails CHEATSHEET docker-compose run --rm web rails new ....
熟悉Ruby的基本命令参考-现代和近乎纯正的OOP! ◈◈ 可以在找到 PDF 格式的清单表,或,而下面是不规则的 html 格式。 此参考表是从系统的构建的。目录哈希值可变关键字参数玫瑰树班级修饰符: public, private, ...
gem 相关介绍Ruby-Rails-Resources Ruby and Rails,Web 开发从入门到提高各种相关资源和书籍豌豆荚文案风格指南terminal-mac-cheatsheet Mac terminal 快捷键chrome-cheatsheet Chrome 的快捷键alfred-workflows ...
短跑速查表 可以从“首选项”>“下载”>“备忘单”中安装的所有备忘单都可以在此存储库中找到。 您可以使用拉取请求修改它们或添加新的请求。 备忘单也可以从在线获得。 自己做 单是使用生成的。...
bud, Prototype芽运行时( Bloom ) 我是 Bud,a.k. 。 ,开发,。这是一个在一个,的初始切割,使用 ruby 作为一个设置。...md中的语言 cheatsheet ;请参阅文档/目录以获取其他文档。目前的主要缺陷是:
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet Emmet 7个很棒的Emmet HTML省时技巧 快速的工作流程:掌握Emmet 备忘单 文件 网页颜色 ColorSafe-色彩对比 喜欢这个网站的调色板 CSS ...
备忘单幻灯片! 来自GitHub学习实验室(GitHub课程简介)[ ]的代码。 带有几张备忘单的自定义幻灯片->