I'm new in ruby (and in programming too)
I have built this code:
#This method executing a url and give the response in json format
def get url
return JSON.parse(RestClient::Request.execute(method: :get, url: url))
end
And now I'm trying to handle a case that the response code from any of the urls is not ok, and I want to replace it with error message "error"
I have tried to replace the get method with this code:
def get url
if ((RestClient::Request.execute(method: :get, url: url)).code == 200)
return JSON.parse(RestClient::Request.execute(method: :get, url: url))
else
error = "error"
return error.as_json
end
end
But if the response from the url is not 200 I get an error message "406 not acceptable" instead of "error"
Thanks in advance
def get url result = RestClient::Request.execute(method: :get, url: url) JSON.parse(result) rescue RestClient::Exception => e e.response.body e.response.code end
– Hammers