Search code examples
rubyhttphttparty

Ruby curl Vs. httparty subscribe to a push API streaming


There is an API endpoint that I'm trying to subscribe to. When I do curl -vL 'url' I get streaming of data every 5 seconds and the connection stays open.

When I try to do the same thing from httparty

response = HTTParty.get('url', follow_redirects: true) just halts and doesn't do anything

when I do

response = HTTParty.head('url', follow_redirects: true)

I get

ERR_596_SERVICE_NOT_FOUND

Is there any recommendations on how do that from httparty, other gems or ruby in general?


Solution

  • I was able to do that with typhoeus gem

    request = Typhoeus::Request.new("url", followlocation: true)
                request.on_headers do |response|
                  if response.code != 200
                    raise "Request failed"
                  end
                end
                request.on_body do |chunk|
                  puts "****************"
                  puts chunk.inspect
                  puts "****************"
                end
                request.on_complete do |response|
                  # downloaded_file.close
                  # Note that response.body is ""
                  puts "connection got closed maged"
                end
                request.run