How to properly deal with urls in query string with rails? - ruby-on-rails

I have a base url that i want to redirect the user to, but this url have another url in its query param
for ex:
base_url = "https://www.base.com?origin=#{origin_url}"
the origin url itself have some query params
origin_url = 'https://www.origin.com?utm_source=foo&utm_term=bar'
but there is a problem with that, if I just use this url it comes like this
https://www.base.com?origin=https://www.origin.com?utm_source=foo&utm_term=bar
with that url, the &utm_term becomes a query string for the base url, and not for the origin url
is there a special type of encoding to deal with this nested query string problem ?
I've tired use URI.encode_www_form_component in the origin_url, and I got
https%3A%2F%2Fwww.origin.com%3Futm_source%3Dfoo%26utm_term%3Dbar
so the Full url became
https://www.base.com?origin=https%3A%2F%2Fwww.origin.com%3Futm_source%3Dfoo%26utm_term%3Dbar
but the problem is that I always decode the url before redirect, so it just get decoded again.

Related

Why is this URL invalid?

http://localhost/#/showinfo/http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fobject%2F1%2Fcollection%2F1
I'm trying to pass a URL thats encoded with encodeURIComponent to another page. but it seems that the URL is invalid.

Use base64 encoded string in url cakephp

here is the solution for php Passing base64 encoded strings in URL
but because of urlencode cakephp problem I cant use it (the solution in there is not good for me)
Passing an urlencoded URL as parameter to a controller / action at CakePHP
Rawurlencode does not work either. Also I cant hash it because I need it to be reversible. Or is there some "reversible" hash function. I know that the meaning of hash is for one way, but like some way to get output similar(so I can use in the url without problems) to md5/sha* but be able to reverse.
QSN: how to use base64 encoded string in url to avoid problems, mentioned in the above link.

Get Return URL Query String Value having #

I am working on an application that uses Angular.js and ASP.NET MVC. The routes for angular.js contain a '#'. Some pages in the application can be viewed without needing to log in. These pages however contain URL's to pages in the application that need the user to be logged in. They look like:
Want this voucher?
So when these links are clicked, MVC forms authentication redirects to the sign in page with the return URL query string that looks like this:
http://localhost:18030/signin?ReturnUrl=%2f#/shop/voucher/6bc1
So on the sign in pages, when i try to look at the query string I only get '/' for return URL as that is the %2f part. I lose the remaining value because of the #.
If I try to URL encode the link, the # and other '/' characters do get encoded but then the routing gets messed up and I get 404 errors.
Is there anyway to access the full query string value with the '#' in it? I am not sure why forms authentication does not encode the #.
The url fragment is never sent to the server, so you have to get a little creative here.
One way I have solved this in the past is to use client side code to modify the action URL for the login, and append the fragment as a query string parameter.
//Pulls out everything past the '#'
var fragment = $location.path();
$scope.fragmentParam = "&fragment=" + fragment;
You can append that query string param to your form action, and then handle it on the server side in order to redirect the user appropriately.
public ActionResult Login(MyLoginInfo info, string returnUrl, string fragment){
//Normal login code
var redirectUrl = String.Format("{0}#{1}", returnUrl, fragment);
Redirect(redirectUrl);
}
This is a simplified version, but you should be able to get the general idea. You need to pass that information to the server, and then reconstruct a redirect url that includes the fragment.

How do I pass a URL as Action Parameter?

I have an action that expects a string containing a path to a resource ie "/123/something.html"
I am trying to user HttpUtility.UrlEncode() to encode the parameter but then the slashes are encoded to contain '%' characters and this results in a 400 error from the server. How can work around this in ASP.NET MVC2?
you certainly can pass those characters in a URL but they just have to be passed in the query string in the part after the question mark
Ex : http://localhost/servercrm/contacts/query?search=%25%20%25

How this URL will be decoded - Confusion in basic Http Get

Basically I need to pass three paramaters to a http as get. Here are the parameters
param1 = 3
param2 = 454
param3 = http://localhost:3000/another_test?another_param=4&another_param2=978
This transforms to
http://localhost:3000/test?param1=3&param2=454&param3=http://localhost:3000/another_test?another_param=4&another_param2=978
I am just confused whether the URL formed is correct or not. Will this work or is there anyother way to do this. I am using Rails. I did a decode and clicked on the link and I still see the above URL coming. Will this work on the receiever side, meaning will it be decoded as I had intended.
Please advise.
It should work as long as you url encode the params. In that case the & and ? will be transformed, making it possible for Rails to differentiate between the query string parameters and the query string delimiters.
To ensure that it is encoded you can use Rack::Utils.escape or Hash#to_query.
This will be decoded as:
param1=3
param2=454
param3=http://localhost:3000/another_test?another_param=4
another_param2=978
You need to encode param3, or at minimum replace the ampersands in it with the correct URL encoding, in order for it to match back up to your input parameters.

Resources