How to read content of remote file in Ruby on Rails?

断了今生、忘了曾经 提交于 2019-12-23 07:09:26

问题


here my file: http://example.com/test.txt

i have to read content of http://example.com/test.txt (a JSON string) and parse it in Ruby


回答1:


I would suggest using open-uri:

require 'json'
require 'open-uri'
result = JSON.parse open('http://example.com/data.json').read



回答2:


require 'net/http'
uri = URI('http://my.json.emitter/some/action')
json = Net::HTTP.get(uri)

json will contain the JSON string you fetched from uri.

Then read this StackOverflow post.



来源:https://stackoverflow.com/questions/8149366/how-to-read-content-of-remote-file-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!