How can i connect to a separate server and parse its JSON response?

前端 未结 2 1009
谎友^
谎友^ 2021-01-26 13:02

i need to connect to another server we manage and have it\'s results (in JSON format) processed by rails... how can I do it ?

Thanks!

2条回答
  •  渐次进展
    2021-01-26 13:31

    With the NET/HTTP library, you can get the content of a distant file.
    Then with the JSON library you can parse a json string into a hash.

    def get_datas
        url = URI.parse("http://www.example.com/page.json")
        res = Net::HTTP.start(url.host, url.port) {|http|
            http.get(url.path)
        }
        JSON.parse res.body
    end
    

提交回复
热议问题