开发者

Typhoeus::Request.new(...) doesn't work, but Typhoeus::Request.get(...) does!

I am using Ruby on Rails 3. I just installed Typhoeus and I am tryng to make a HTTP request like this

  require 'typhoeus'

  ....
  request = Typhoeus::Request.new("http://google.com",
              :method        => :get,
              :params        => {
                :email       => "test@test.com",
                :pas开发者_Go百科sword    => "test"
              }
            )

  resp = request.response

but I have a problem: debug of resp is always blank and this happens also if I don't use options (method, params, ...).

However, if I use the following code, it will work:

   resp = Typhoeus::Request.get("http://google.com?email=test@test.com&password=test")

and I will get values for resp.

What can be the problem?


What I use

  • Mac OS with "Snow Leopard" v 1.1.6

  • MacPorts - The version 7.21.2 of libcurl was already installed from this software

  • RVM (Ruby Version Manager)


P.S.: if you need more info, let me know.


In the official documentation there are some (overwrited) alerts for Mac Os users related to the installation.

Terminal outputs:

$ which ruby
/Users/<my_user_name>/.rvm/rubies/ruby-1.9.2-p136/bin/ruby

$ which curl
/opt/local/bin/curl


Typhoeus::Request.get

and its friends post, put, delete, head, patch are only shortcuts and will fire a request immediately. If you create a request by hand, you have to run it afterwards:

request = Typhoeus::Request.new("www.example.com")
request.run
#=> <Typhoeus::Response ...>

I do not suggest to use a hydra for single request because thats going to slow you down. Here is the documentation: http://rubydoc.info/github/typhoeus/typhoeus/Typhoeus/Request.


You need to run the request in a Hydra:

request = Typhoeus::Request.new
hydra = Typhoeus::Hydra.new
hydra.queue(request)
hydra.run
request.response #=> "response"

I monkey patched Typhoeus so that it will automatically queue up the response in a Hydra if it hasn't been ran already.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜