我使用Rails 4.2.7。如何向Net::HTTP::Post请求添加头?我试过了
params = {"SubmitButton" => "View"}
…
headers = {"Referer" => url}
req = Net::HTTP::Post.new(uri, params.to_query, headers)
字符串
但是当我运行这段代码时,我得到了错误
Error during processing: wrong number of arguments (given 3, expected 1..2)
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/request.rb:14:in `initialize'
/Users/davea/Documents/workspace/myproject/app/services/marathon_guide_race_finder_service.rb:97:in `new'
/Users/davea/Documents/workspace/myproject/app/services/marathon_guide_race_finder_service.rb:97:in `block in process_race_link'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb:187:in `block in each'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb:186:in `upto'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb:186:in `each'
型
注意我想在对象被发送之前将头添加到对象中。
2条答案
按热度按时间nsc4cvqm1#
您可以使用示例来创建带有标题的帖子
字符串
vxbzzdmp2#
除了T J的方法(将头作为参数提供给
post()
调用)之外,您还可以使用:[]
在HTTPRequest
对象本身上设置头:字符串
来源:https://ruby-doc.org/stdlib-2.3.8/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Setting+Headers
这允许您在执行请求之外设置标头。
同样的方法也适用于
Net::HTTP::Post
对象:型