Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get redirect of a URL in Ruby

According to Facebook graph API we can request a user profile picture with this (example):

https://graph.facebook.com/1489686594/picture 

But the real image URL of the previous link is:

http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg

If you type the first link on your browser, it will redirect you to the second link.

Is there any way to get the full URL (second link) with Ruby/Rails, by only knowing the first URL?

(This is a repeat of this question, but for Ruby)

like image 356
neon Avatar asked May 03 '11 15:05

neon


People also ask

What is redirect_to in Ruby?

redirect_to(options = {}, response_options = {}) LinkRedirects the browser to the target specified in options . This parameter can be any one of: Hash - The URL will be generated by calling url_for with the options .

What does redirect_to return?

redirect_to will cause any automatic rendering to be skipped. You only need the 'return' if you need to bypass further code in the action. If the further code does an explicit render , then you must do a return to avoid an error of redirect and render both being present.

What is Unvalidated Redirects and Forwards?

Unvalidated Redirects and Forward Vulnerability, also sometimes referred to as URL Redirection Vulnerability, is a type of bug found in the Web Application. In this type of vulnerability, the attacker uses to manipulate the URL and sends it to the victim.

How do I redirect back in rails?

In Rails 4. x, for going back to previous page we use redirect_to :back. However sometimes we get ActionController::RedirectBackError exception when HTTP_REFERER is not present. This works well when HTTP_REFERER is present and it redirects to previous page.


1 Answers

This was already answered correctly, but there's a much simpler way:

res = Net::HTTP.get_response(URI('https://graph.facebook.com/1489686594/picture')) res['location'] 
like image 120
FelipeC Avatar answered Oct 06 '22 21:10

FelipeC