How to pass a URL in params? - ruby

I have a url, "localhost/test/http://myimage.com/" (I'm passing myimage.com, because it's hosted on another site and I'm accessing it via an api) my question is how do I go about encoding the image portion of the URL? I thought about doing a gsub on the '.' and '/' and then gsubing them back, but I'm wondering if there's an easier way. Thanks for your help.

You could use URI::encode_www_form_component(str) and URI::decode_www_form_component
Check: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html

You can use the uri library to escape and unescape a url
require 'uri'
escaped = URI.escape(data, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
and you can get the data back with
original = URI.unescape(escaped)

Related

Is there a DNN URL decode method as it is changing a URL Encode

We are using Server.URLEncode to change an SKU with a forward slash from BDF5555/45 to BD5555%2F45 in our Href on a button.
When a person clicks on the button the page navigates to another module which has Request.QueryString but DNN is changing the URL.
How can I get the variable decodeprodCode to include the &45 as BDF5555/45?
Perhaps DNN is rewriting the URL?
There is a NavigateURL class in DotNetNuke.Common.Globals that will generate a correct url based on a TabID and a lot of overloads.
DotNetNuke.Common.Globals.NavigateURL(TabId)
You can also use querystring parameters
DotNetNuke.Common.Globals.NavigateURL(TabId, ControlKey,"key=value"))
DNN by default will re-write querystring values into /key/value format in the URL assuming that it is properly encoded. For example if you have querystring values of sku=1 and productid = 2 the resultant URL will be
https://yoursite.com/Your-Page/sku/1/productid/2
This is done via the FriendlyUrlProvider, but it should not have any impact to your ability to process via Request.Querystring as this is a very, very common practice for passing values into DNN.
Your code to retrieve the value is correct.
I ended up using the following code instead of a Request.Querystring.
string RawurlFromRequest = Request.RawUrl;
var cleanSKU = RawurlFromRequest.Split(new[] {"sku/"}, StringSplitOptions.None)[1];
var CleanSKUNoOtherQueryStrings = cleanSKU.Split(new[] {"&"}, StringSplitOptions.None)[0];
The Request.RawURL brings back the URL with the special characters as it is without encoding. As Mitchel Sellers mentioned above, DNN uses the FriendlyURLProvider which rewrites the URL.
For example www.mysite.com/ProductFilter/SKU/BDF5555/45 and not
www.mysite.com/ProductFilter/SKU/BDF5555%2F45
The CleanSKU variable will look for SKU/ and split everything on the left as it is set to [1].
After everything has been split on the left, we look for other QueryStrings which we usually add with a & sign. We will split everything on the right by setting it to [0].
This will bring back BDF5555/45 in the backend with a forward slash which we can use to retrieve product information from our ERP system which URLdecode couldn't do.

Using special characters in open-uri requests

I'm using open-uri to get content from a page on the web to be used with nokogiri.
I'm trying something like:
url = "http://pesquisa.bvsalud.org/portal/?output=site&lang=pt&from=0&sort=&format=summary&count=20&fb=&page=1&q=\"qualidade+de+vida\"&index=tw"
response = open(url)
Then I get the error: URI::InvalidURIError: bad URI(is not URI?)
The catch is: I know I can use URI.encode(url) to prevent some special characters in the url, but the website I'm requesting doesn't give me the same response when I sanitize the url, it doesn't answer properly when using '%22' instead of double quotes..
How can I make such request using double quotes? Any other library that can do it? Open-uri doesn't accept that. I tryed to use the gems addressable-uri and eat, but I get the same error on both. :/
URI.encode('http://pesquisa.bvsalud.org/portal/?output=site&lang=pt&from=0&sort=&format=summary&count=20&fb=&page=1&q=\"qualidade+de+vida\"&index=tw')
=> "pesquisa.bvsalud.org/portal/?output=site&lang=pt&from=0&sort=&format=summary&count=20&fb=&page=1&q=%5C%22qualidade+de+vida%5C&index=tw"

How can I convert a relative link in Mechanize to an absolute one?

Is there is a way to convert a Mechanize relative-link object to another one which contains the absolute URL.
Mechanize must know the absolute link, because I can call the click method on relative links too.
You can just merge the page uri (which is always absolute) with the link uri:
page.uri.merge link.uri
This is not specific to Mechanize, but an easy way would be to use the base URL in the <base> tag and add it to the relative URL to use for whatever purpose you want. This generally works.
But, then I'm not sure if you could call the click method on that since I don't know Mechanize that well.
You can also use resolve
Example:
require 'mechanize'
agent = Mechanize.new
page = agent.get(url)
some_rel_url = '/something'
url = agent.resolve(some_rel_url)
Keep in mind that the other answers provided do not take into account all the possibilities to get the base url as described here
Basically this:

Problem with `&` in Net::HTTP::Get.new

I'm using net/http in Ruby to send a GET request to a web service. The important bit of code is the following:
Net::HTTP.start("www.thehost.com") do |http|
req = Net::HTTP::Get.new("/api" + ROUTES[sym] + "?" + params)
resp = http.request req
end
params is a string which contains key-value pairs in the form key=val&blag=blorg. When the response comes in, it turns out to be an error page from the server, which quotes the request URI; instead of key=val&blag=blorg, it has key=val&blag=blorg. Since when I enter the same address into a web browser with & instead of &, I get the expected response, I suspect that the escaping of & is what's causing the problem. If anyone with more experience disagrees with that, however, feel free to rename my question!
I should note that when I use http://www.thehost.com/api#{ROUTES[sym]}?#{params} with Net::HTTP.get_response, I get the expected response. What can I do to fix this?
Just a wild guess: are you doing this in Rails, and could its XSS protection/HTML escaping features be the culprit? What happens if you change it to params.html_safe?
Since using #{...} in one case works, what happens if you do "/api#{ROUTES[sym]}?#{params}" instead of concatenating strings with +?

question marks in sinatra get method

I am trying to use a question mark in one of my url's but sinatra/ruby is interpreting it as the regex character that makes precedings optional. Is there any way to allow actual ? in your get methods? I have tried \? and [?] but they didn't work. Here is the begining of my get method:
get '/group?groupid=:groupId' do |id|
If I go to www.mydomain.com/group?groupid=1 I get an error but it works if I go to www.mydomain.com/groupgroupid=1
The "?" starts the querystring portion of the URL; querystring parameters are accessible via the "params" Hash.

Resources