[英]http request and get a json for response in rails
In a rails project, I want send a http request and get a json in response and save it to database. 在Rails项目中,我想发送一个http请求并获取一个json作为响应并将其保存到数据库。 I send http request like below:
我发送如下的http请求:
def test_json
#url is a address like: http://192.32.10.18:8080/GetJson?serial=4306341
uri = URI.parse(url)
response = Net::HTTP.get_response(uri)
end
Now, I want save this json to database and then show to user, How can I do this? 现在,我想将此json保存到数据库,然后显示给用户,我该怎么做?
To get the response body you'd use: 要获得响应正文,请使用:
uri = URI.parse(the_url)
response = Net::HTTP.get_response(uri)
At this point response
is your response (in your case its a string of JSON) which you can do whatever you want with. 此时,
response
就是您的响应(在您的情况下为JSON字符串),您可以执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.