0

I'm just dipping a toe in the water with Crystal at the moment and, as an exercise, trying to port one of my Python scripts across.

The script in question downloads the 'latest' PDF from a URL which takes the form: "http://somesite.com/download/latest/". When visited that URL automatically redirects to the page for the latest download eg. "http://somesite.com/download/4563/"

I'm having difficulty working out how to implement this in Crystal so that I can grab the actual URL that the redirect ends up on.

In Python I do:

currenturl = urllib.request.urlopen(latesturl)
#above will redirect to URL of format http://somesite.com/download/XXXXX/
#where XXXXX is the current d/load
endurl = currenturl.geturl()

...which gives me the end URL in the "endurl" variable.

But, reading the docs for Crystal's "http/client" I can't see any way to return the actual URL that a redirect ends up on. Is it possible?

vlota
  • 3
  • 3

1 Answers1

1

Crystal's HTTP::Client currently can't automatically follow redirects.

Please note that you're reading an outdated version of the API docs, the current is at https://crystal-lang.org/api/latest/HTTP/Client.html (I don't think there have been relevant changes between 0.24.1 and 0.26.1 though).

But you can easily access the redirect URL from reading the Location header of an HTTP response:

response = HTTP::Client.get latesturl
endurl = response.headers["Location"]
Johannes Müller
  • 5,581
  • 1
  • 11
  • 25
  • Great. Thanks! One small puzzler stil though: `response.headers["Location"]` returns the relative path to the endURL, rather than the full URL. ie. I get `/download/4563/` instead of `http://somesite.com/download/4563/`. No biggie as I can store the domain in a var and join it back on, but is there a built-in way to get the full URL? – vlota Sep 11 '18 at 12:47
  • No. This is just directly retrieving the plain value from the HTTP API. As mentioned `HTTP::Client` does not yet understand the concept of redirects and thus won't provide a fully qualified URL for you. – Johannes Müller Sep 11 '18 at 12:58