2

I'm trying to pass a URL as a query string so it can be read by another website and then used:

www.example.com/domain?return_url=/another/domain

Gets returned as:

www.example.com/domain?return_url=%2Fanother%2Fdomain

Is there a way this URL can be read and parsed by the other application with the escaped characters?

The only way I can think of is to encode it somehow so it comes out like this:

www.example.com/domain?return_url=L2Fub3RoZXIvZG9tYWlu

then the other application can decode and use?

https://www.base64encode.org/

2 Answers 2

5

www.example.com/domain?return_url=%2Fanother%2Fdomain

This is called URL encoding. Not because you put a URL in it, but because it encodes characters that have a special meaning in a URL.

The %2F corresponds to a slash /. You've probably also seen the %20 before, which is a space .

Putting a complete URI into a URL parameter of another URI is totally fine.

http://example.org?url=http%3A%2F%2Fexample.org%2Ffoo%3Fbar%3Dbaz

The application behind the URL you are calling needs to be able to understand URL encoding, but that is a trivial thing. Typical web frameworks and interfaces to the web (like CGI.pm or Plack in Perl) will do that. You should not have to care about it a all.

To URL-encode something in Perl, you have several options.

You could use the URI module to create the whole URI including the URL encoded query.

use URI;

my $u = URI->new("http://example.org");
$u->query_form( return_url => "http://example.org/foo/bar?baz=qrr");

print $u;

__END__
http://example.org?return_url=http%3A%2F%2Fexample.org%2Ffoo%2Fbar%3Fbaz%3Dqrr

This seems like the natural thing to do.

You could also use the URI::Encode module, which gives you a uri_encode function. That's useful if you want to encode strings without building a URI object.

use URI::Encode qw(uri_encode uri_decode);
my $encoded = uri_encode($data);
my $decoded = uri_decode($encoded);

All of this is a normal part of how the web works. There is no need to do Base 64 encoding.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. How would the application I'm sending it to actually use that encoded URL then to redirect elsewhere? :- /domain decides to use that URL to go to http%3A%2F%2Fexample.org%2Ffoo%2Fbar%3Fbaz%3Dqrr for example
Am I right in thinking /domain has to use uri_decode($return_url) and then redirect?
4

The correct way would be to uri-encode the second hop as you do in your first example. The URI and URI::QueryParam modules make this nice and easy:

To encode a URI, you simply create a URI object on your base url. Then add any query parameters that you want. (NOTE: they will be automatically uri-encoded by URI::QueryParam):

use strict;
use warnings;

use feature qw(say);

use URI;
use URI::QueryParam;

my $u = URI->new('http://www.example.com/domain');
$u->query_param_append('return_url', 'http://yahoo.com');

say $u->as_string;
# http://www.example.com/domain?return_url=http%3A%2F%2Fyahoo.com

To receive this url and then redirect to return_url, you simply create a new URI object then pull off the return_url query parameter with URI::QueryParam. (NOTE: again URI::QueryParam automatically uri-decodes the parameter for you):

my $u = URI->new(
  'http://www.example.com/domain?return_url=http%3A%2F%2Fyahoo.com'
);
my $return_url = $u->query_param('return_url');

say $return_url;
# http://yahoo.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.