0

I am looking for a way to check if the API URL exists before making a request using HTTParty. Currently, if the URL does not exist, the HTTParty request errors.

I tried following this Check if URL exists in Ruby:

api_url = "http://api.meetup.com/LA-Computer-Science-Reading-Group/events?photo-host=public&page=20&sig_id=215634693&sig=0be729af948c1a17ce5f35faa9fcedd5ae22de56"
require "net/http"
url_request = URI.parse(api_url)
req = Net::HTTP.new(url_request.host, url_request.port)
res = req.request_head(url_request.path)

@check = res 

response = HTTParty.get(api_url) 

view:

<%= @check.inspect %>
#<Net::HTTPOK 200 OK readbody=true>

But if I change the URL to something that does not exist, such as:

http://api.meeeeeetup.com/LA-Computer-Science-Reading-Group/events?photo-host=public&page=20&sig_id=215634693&sig=0be729af948c1a17ce5f35faa9fcedd5ae22de56` 

The page still errors with getaddrinfo: nodename nor servname provided, or not known

Also, the URL request I want to check is a XML request:

http://api.indeed.com/ads/apisearch?publisher=#{publisher_key}&q=java&l=austin%2C+tx&sort=&radius=&st=&jt=&start=&limit=&fromage=&filter=&latlong=1&co=us&chnl=&userip=&useragent=&v=2

and using the idea above, this API URL does not return a successful call. Instead it returns a #<Net::HTTPMethodNotAllowed 405 Method Not Allowed readbody=true>.

I'm not sure about emulating a browser to check the results that way or if there is an easier way to check for validity of the API URL request before the HTTParty request.

Community
  • 1
  • 1
teresa
  • 356
  • 5
  • 21
  • 1
    Why can't httparty simply make the request and fail if invalid url? – Joel Blum Feb 08 '17 at 19:16
  • @Joel_Blum I found if I pass in the invalid URL to HTTParty, it would just error with `getaddrinfo: nodename nor servname provided, or not known` and break the app. I'm not sure how to catch that error or if HTTParty has a way to check the URL response before failing. – teresa Feb 08 '17 at 19:22
  • 1
    You can check a URL for validity; that's different than ensuring the endpoint *exists*. But your "invalid" URL example isn't an invalid URL, it just doesn't exist. http://stackoverflow.com/a/26768085/438992 – Dave Newton Feb 08 '17 at 19:31
  • Thank you, @DaveNewton. I was able to get the check working based on your comment. I also edited my question so it was more accurate. – teresa Feb 08 '17 at 20:00
  • @tshckr Please add the answer as an answer, not as part of the question. – Dave Newton Feb 08 '17 at 20:30

1 Answers1

0

Thanks to @Dave Newton from the comments I was able to get the check working. I also edited my question to reflect that it was not about the 'validity' of the URL. But it was that the URL does not exist.

Based on: How can I handle errors with HTTParty?.

Solution:

 begin
   response = HTTParty.get(url) 
 rescue HTTParty::Error
   # don´t do anything / whatever
   puts "=========ERROR=============="
 rescue StandardError
   # rescue instances of StandardError,
   # i.e. Timeout::Error, SocketError etc
   puts "=========STANDARD ERROR=============="
 end

Then added a check if !response.nil?, so if the HTTParty request returned an error. response would be nil and the block of code would be ignored.

Community
  • 1
  • 1
teresa
  • 356
  • 5
  • 21