Rails - 发送 GET 请求
Rails - Sending a GET Request
我正在尝试在提交表单时检索用户的 Google 帐户信息。为此,我必须向 url 发出 GET 请求。这是 YouTube API 文档
中的内容
To request the currently logged-in user's profile, send a GET request
to the following URL. Note: For this request, you must provide an
authentication token, which enables YouTube to identify the user.
https://gdata.youtube.com/feeds/api/users/default
https://developers.google.com/youtube/2.0/developers_guide_protocol_profiles?hl=en
如何在提交表单时生成自定义(或特定的)GET 请求,以及如何提供身份验证令牌?可能有帮助的信息:提交表单时,它将转到 VideoController 的新方法。
另外,在提出请求后,我如何访问信息?我需要查看给定信息中是否存在 <yt:relationship>
。
谢谢!
可以使用excon
gem对外部资源进行特定的CRUD
或仅get
请求,例如应该是这样的:
Excon.post('http://youtube.com', :body => 'token=sjdlsdjasdljsdlasjda')
PS:但似乎您不需要直接的 gems,真正使用 google-api
gem,因为它documentation.
中的描述
我发现 HTTParty gem 对于在 rails 中使用外部 API 非常有用。
您可以提出请求
response = HTTParty.post("http://www.example.com", :timeout => 5, :body => "my body content", :query => { hash_of_params_here } ,:headers => {'Content-Type' => 'application/json', 'AuthKey' => "your_key_here"})
response 对象将有 'statuscode'、'headers'、'body' 等(无论 youtube 在您的情况下发回什么)。您可以使用
访问其内容
response.parsed_response
get 请求也一样
请在开始之前阅读更多相关信息以便更好地理解。
资料来源:Documentation & Tutorial
我正在尝试在提交表单时检索用户的 Google 帐户信息。为此,我必须向 url 发出 GET 请求。这是 YouTube API 文档
中的内容To request the currently logged-in user's profile, send a GET request
to the following URL. Note: For this request, you must provide an
authentication token, which enables YouTube to identify the user.
https://gdata.youtube.com/feeds/api/users/default
https://developers.google.com/youtube/2.0/developers_guide_protocol_profiles?hl=en
如何在提交表单时生成自定义(或特定的)GET 请求,以及如何提供身份验证令牌?可能有帮助的信息:提交表单时,它将转到 VideoController 的新方法。
另外,在提出请求后,我如何访问信息?我需要查看给定信息中是否存在 <yt:relationship>
。
谢谢!
可以使用excon
gem对外部资源进行特定的CRUD
或仅get
请求,例如应该是这样的:
Excon.post('http://youtube.com', :body => 'token=sjdlsdjasdljsdlasjda')
PS:但似乎您不需要直接的 gems,真正使用 google-api
gem,因为它documentation.
我发现 HTTParty gem 对于在 rails 中使用外部 API 非常有用。
您可以提出请求
response = HTTParty.post("http://www.example.com", :timeout => 5, :body => "my body content", :query => { hash_of_params_here } ,:headers => {'Content-Type' => 'application/json', 'AuthKey' => "your_key_here"})
response 对象将有 'statuscode'、'headers'、'body' 等(无论 youtube 在您的情况下发回什么)。您可以使用
访问其内容response.parsed_response
get 请求也一样
请在开始之前阅读更多相关信息以便更好地理解。 资料来源:Documentation & Tutorial