简体   繁体   English

Rails http获取请求

[英]rails http get request

I am trying to connect with an external API (via get request). 我正在尝试与外部API连接(通过get请求)。 I use Net Net::HTTP.get(URI.parse(link)). 我使用Net Net :: HTTP.get(URI.parse(link))。 Suppose that link is the link I want to get request from. 假设该链接是我要从中获取请求的链接。 The problem is that when I run this request does not do what it has to do ( authenticate a user, based on some passwords and tokens); 问题是,当我运行此请求时,它不执行它必须做的事情(根据某些密码和令牌对用户进行身份验证); but if I use manually this link ( via browser) it works. 但是,如果我手动使用此链接(通过浏览器),它将起作用。 Any ideas? 有任何想法吗? Thank you in advance. 先感谢您。

PS I am completely new to Rails PS我对Rails完全陌生

There are a lot of resources on what you are trying to accomplish. 您尝试完成的工作有很多资源。 What you are doing, is using "Ruby", not rails, to send a request to an external provider using the HTTP GET method. 您正在执行的操作是使用“ Ruby”(而不是rails)使用HTTP GET方法将请求发送到外部提供程序。

If I may make a suggestion - which will accomplish that task - 如果我可以提出建议-可以完成该任务-

Net::HTTP.get_response(url, path_to_svc) 净:: HTTP.get_response(URL,path_to_svc)

Your code would look like - 您的代码看起来像-

json = Net::HTTP.get_response("apps.db.ripe.net", "/whois/lookup/ripe/inetnum/127.0.0.1.json").body
puts json

this was tested, and works. 经过测试,可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM