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.
Related
For a project I'm working on in Rails, I'm using AlchemyData news API. I'm currently trying to run an HTTP request in pry, which looks like below (I replaced my own API key with the substitute my_api_key, and for clarity, a > indicates what I've entered and => what was returned to me):
> uri = URI.parse("https://gateway-a.watsonplatform.net/calls/data/GetNews?apikey=MY_API_KEY&outputMode=json&start=now-24h&end=now&q.enriched.url.title=A[Kim^Kardashian]&q.enriched.url.docSentiment.type=positive")
=> #<URI::HTTPS https://gateway-a.watsonplatform.net/calls/data/GetNews?apikey=MY_API_KEY&outputMode=json&start=now-24h&end=now&q.enriched.url.title=A[Kim^Kardashian]&q.enriched.url.docSentiment.type=positive>
> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP gateway-a.watsonplatform.net:443 open=false>
> response = http.request(Net::HTTP::Get.new(uri.request_uri))
=> #<Net::HTTPBadRequest 400 Bad Request readbody=true>
I'm not understanding why I keep getting a "400 Bad Request" error. From my understanding, that is (usually) returned when there is an error with the url, but when I run mine it works perfectly. Is there something wrong with my syntax or is there a different error? If so, what is it? How can I fix this?
Fixed this by using an HTTP gem (https://github.com/httprb/http) and using that to handle my HTTP request, like so:
result = HTTP.get("my_url_here")
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
I am making an Oauth call to the Facebook API to get myself an access_token
def access_token
token_uri = URI("https://graph.facebook.com/oauth/access_token?client_id=#{CLIENT_ID}&client_secret=#{CLIENT_SECRET}&grant_type=client_credentials")
token_response = HTTParty.get(token_uri)
Rails.logger.info(token_response)
return token_response
end
I get a response and a access_token generated, let's say it's
access_token=123456789|abcdefghijk
but when I then try to use this token
def get_feed
fb_access_token = access_token
uri = URI("https://graph.facebook.com/#{VANDALS_ID}/posts/?#{fb_access_token}")
end
I get an error
URI::InvalidURIError: bad URI(is not URI?)
and the uri generated stops at the | even though there are more characters after the pipe to complete my access_key
https://graph.facebook.com/id-here/posts/?access_token=123456789|
How do I get the full access token available in my URI?
The reason you are getting an error is that | symbol is not allowed in the proper URI, hence it needs to be escaped before it's parsed. URI comes with a method to do it for you:
uri = URI(URI.escape "https://graph.facebook.com/#{VANDALS_ID}/posts/?#{fb_access_token}")
uri.to_s #=> https://graph.facebook.com/id-here/posts/?access_token=123456789%7Cabcdefghijk
When the url is requested, the server should automatically decode it, so all should work as expected.
I'm trying to use an external API to get some information
address=self.address
response = HTTParty.get("http://api.map.baidu.com/geocoder/v2/?address=#{address}&output=json&ak=5dfe24c4762c0370324d273bc231f45a")
decode_response = ActiveSupport::JSON.decode(response)
However, the address is in chinese, so I need to convert into UTF-8 code,
if not I'll get URI::InvalidURIError (bad URI(is not URI?): How to do it?
I tried address=self.address.force_encoding('utf-8'), but it does not work, maybe I should use other method instead?
UPDATE:
uri = "http://api.map.baidu.com/geocoder/v2/?address=#{address}&output=json&ak=5dfe24c4762c0370324d273bc231f45a"
encoded_uri = URI::encode(uri)
response = HTTParty.get(encoded_uri)
decode_response = ActiveSupport::JSON.decode(response)
self.latitude = decode_response['result']['location']['lat']
and I get can't convert HTTParty::Response into String. What's wrong with it?
I found something here, I think I'll need to tell Httparty explicityly to parse it with JSON?
You could save this to a file 'geo.rb' and run ruby geo.rb
require 'uri'
require 'httparty'
address = "中国上海"
uri = "http://api.map.baidu.com/geocoder/v2/?address=#{address}&output=json&ak=5dfe24c4762c0370324d273bc231f45a"
encoded_uri = URI::encode uri
puts HTTParty.get(encoded_uri)
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