URL get response error - ruby-on-rails

I have the following lines in my Rails controller code
url_parsed = URI.parse(url)
response = Net::HTTP.get_response(url_parsed)
When I passed www.google.com as url, it gave
undefined method `request_uri' for #<URI::Generic:0x00000002e07908 URL:www.google.com>
on the second line, even though I don't call the method request_uri anywhere.
In this case, I'd like it to show my nice error page, instead of this ugly error. How can I do it?

your url string is missing a protocol: url="http://www.google.com"
Then your code will return Net::HTTPOK - see this documentation

Related

ERROR STATUS: URL contains outer http

I have a COLDFUSION page which except parameters from url and show them in fields. My url looks like this which is working.
http://www.example.com/test.cfm?activeUrl=www.msn.com&secure=False
But following is not working. I have added http before www in activeUrl value.
http://www.example.com/test.cfm?activeUrl=httpwww.msn.com&secure=False
It is giving me following error. "ERRROR STATUS: URL contains outer http"
Can any one help me to solve this problem?
For me, It seems that something related to iis configuration.
The string in httpwww.msn.com is causing your browser error. It should be http://www.msn.com, but should also be URL encoded.
ohh, i have not checked the application.cfc ONREQUESTSTART method. it has some condition for query string, which is showing this error. :(

How to handle "NetworkError: 400 Bad Request" in rails?

I am facing an error when a user enters search string as '%' in url itself. After entering my url looks as
http://localhost:3000/search/%
Now its showing "NetworkError: 400 Bad Request - http://localhost:3000/search/%". I want to redirect to some other page and show, a bad request image when the error occurs. Please suggest me some ideas to do this.
I have attached the error image.
UPDATE
I have tried the below.
config/application.rb:
config.exceptions_app = self.routes
config/routes.rb:
match "/400", :to => "errors#bad_request"
It's not coming inside the bad_request method.
In Log i am getting error as
Invalid request: Invalid HTTP format, parsing fails.
Have you checkout this gem 'utf8-cleaner' which removes invalid UTF-8 characters from the environment so that your app doesn't choke on them.
If you want custom error pages: then look at this answer

Elasticsearch URL problems

I am trying to return more values using elastic search and i am getting an error with this url.
The error just says error the URL i am trying to post to is:
http:/someserver.com/elasticSearch/media/_search?q=bowls-d{'from' : 0, 'size' : 100}
I am not sure what is wrong with the URL this, URL does work but i only get the default 10 hits back:
http:/someserver.com/elasticSearch/media/_search?q=bowls
Any ideas?
Try this:
http:/someserver.com/elasticSearch/media/_search?q=bowls&from=0&size=100

Ruby Net::HTTP can't get url right

The code:
self.response = Net::HTTP.get_response(URI.parse(url.strip))
The url:
http://www.apmebf.com/g7116vpyxF/pw0/EDDEGMFH/EGJFDKJ/D/D/D?h=q<<lxxt%3A%2F%2F000.htfspz0.rix%3AC4%2Fgpmgo-57A64BA-54457D68<<K<<
Gives this error:
bad URI(is not URI?)
I tried using URI.encode but then it throws this error:
undefined method `request_uri' for #<URI::Generic:0x007fc7ae31ecf0>
It's driving me nuts -- any ideas?
If you urlencode the URL you get
http%3A%2F%2Fwww.apmebf.com%2Fg7116vpyxF%2Fpw0%2FEDDEGMFH%2FEGJFDKJ%2FD%2FD%2FD%3Fh%3Dq%3C%3Clxxt%253A%252F%252F000.htfspz0.rix%253AC4%252Fgpmgo-57A64BA-54457D68%3C%3CK%3C%3C
Ruby can't find a protocol in there, so it assumes it is a generic URI and not an HTTP-URI. Only HTTP-URIs have a request_uri method.
You'll have to fix that URI manually by replacing the < with %3C via str.replace.

405 Method not allowed on Net::HTTP request [ruby on rails]

I'm trying to verify if there is a remote url with following code:
endpoint_uri = URI.parse(#endpoint.url)
endpoint_http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
endpoint_request = Net::HTTP::Head.new(endpoint_uri.request_uri)
endpoint_response = endpoint_http.request(endpoint_request)
I'm still getting 405 Method not allowed. When I use Get instead Head in Net::HTTP::Head.new I'm getting 200 Success but also with whole remote document in response what results in bigger response time (0.3s => 0.9s).
Any ideas why this is happening? Thx
There's a chance that the #endpoint url you're trying to interact with doesn't support HEAD requests (which would be really weird, but still may be the case). Your code works fine for me with a handful of urls (google.com, stackoverflow.com, etc.)
Have you tried a curl request to see what it returns?
curl -I http://www.the_website_you_want_to_test.com

Resources