【问题标题】:Ruby curl Vs. httparty subscribe to a push API streaming红宝石卷曲VS。 httparty 订阅推送 API 流
【发布时间】:2018-08-17 16:56:03
【问题描述】:

我正在尝试订阅一个 API 端点。当我做 curl -vL 'url' 我每 5 秒收到一次数据流,并且连接保持打开状态。

当我尝试从httparty做同样的事情时

response = HTTParty.get('url', follow_redirects: true) 只是停下来不做任何事情

当我这样做时

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

我明白了

ERR_596_SERVICE_NOT_FOUND

对于如何从httparty、其他宝石或一般红宝石那里获得任何建议?

【问题讨论】:

  • HTTParty 本身不支持持久连接。你可以看看persistent_httparty 虽然我从来没有使用过这个库

标签: ruby http httparty


【解决方案1】:

我可以通过 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2012-12-10
    • 1970-01-01
    • 2011-04-02
    • 2019-08-09
    相关资源
    最近更新 更多