我正在使用Rails 4.2.7。如何获取Net :: HTTP :: Post请求中设置的参数?我在我的Rails控制台中试过这个
2.3.0 :001 > url = "https://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP/Post.html"
=> "https://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP/Post.html"
2.3.0 :002 > uri = URI(url)
=> #<URI::HTTPS https://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP/Post.html>
2.3.0 :003 > params = {"submitbutton" => "View"}
=> {"submitbutton"=>"View"}
2.3.0 :004 > req = Net::HTTP::Post.new(uri)
=> #<Net::HTTP::Post POST>
2.3.0 :005 > req.set_form_data(params)
=> "application/x-www-form-urlencoded"
2.3.0 :008 > req.uri.params
NoMethodError: undefined method `params' for #<URI::HTTPS:0x007fe2aefba980>
Did you mean? parser
from (irb):8
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:110:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/davea/Documents/workspace/myproject/bin/rails:9:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.3.0
我也试过调用“req.params”并获得相同的“未定义方法错误。”
答案 0 :(得分:0)
请检查:
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, 'submitbutton' => 'View')
puts res.body
参考: https://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html