Quick and easy HTTP headers with HTTPartyUPDATE: I changed the gem. It now follows redirects and maintains the HEAD request across all redirects.
I have a method that checks URLs submitted by the user to ensure they are valid. I don’t want to scrape the page to make sure any specific text is on there because the URLs can go almost anywhere, so I basically only care if the URL returns an HTTP
I’m using the excellent HTTParty Ruby gem to make my external requests, and it offers a handy
.headmethod that only returns the website headers for the request:
GET, which isn’t ideal since the
GETrequest will download the entire page’s content rather than just the headers, and is understandably slower.
Thankfully, there are a couple options that can be passed to the
.headmethod which takes care of this:
Now, I get just the headers, and since all I care about is whether it was a successful request, I’m able to call
.success?in my method and use the return value.
There are a few external HTTP gems out there - Net:HTTP (of which HTTParty is a wrapper), RestClient, etc - but HTTParty is my favorite so far, and easiest to work with. Check it out if you haven’t already.