Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a URL is decoded/encoded?

I am using Javascript method decodeURIComponent to decode an encoded URL. Now I am having an issue, that sometimes the URL is get encoded twice during redirection between servers, sometimes it is encoded only once.

I want to check that if the URL is still encoded after calling the method decodeURIComponent. How can I do that? Any pointer would be very helpful to me.

Update - 1

If I recursively call a method and check that if the given URL still contains "%", if it contains "%" then decode it and call the method again; and if not return it to the caller, will that work?

Update - 2

For my case I have:

callBackUrl=http%253A%252F%252Fadbc.com%252FPOSM%252Fapp%252Fpages%252Fadf.task-flow%253Fadf.tfDoc%253D%25252FWEB-INF%25252Ftask-flows%25252Fcatalog-edit-task-flow.xml%2526adf.tfId%253Dcatalog%2526_adf.ctrl-state%253Db9akorh22_9%2526articleReference%253D10C00135%2526previousView%253Dcatalog-home%2526fromUCM%253Dtrue%2526articleType%253Dposm%2526developer%253Dcentral 

Now I am taking the value of the callBackUrl in my js method, then decoding it and firing window.open() with that decoded URL. the parameters are same and it has:

  • adf.tfDoc
  • adf.tfId
  • articleReference
  • previousView
  • fromUCM
  • articleType
  • developer

Parameters into it. So I know there is no query string like value="%..".

Update - 3

I have written the following method:

var decodeURLRecursively = function(url) {     if(url.indexOf('%') != -1) {         return decodeURLRecursively(decodeURIComponent(url));     }      return url; } 
like image 669
Tapas Bose Avatar asked Jul 10 '13 07:07

Tapas Bose


People also ask

How can I tell if a URL is encoded?

So you can test if the string contains a colon, if not, urldecode it, and if that string contains a colon, the original string was url encoded, if not, check if the strings are different and if so, urldecode again and if not, it is not a valid URI.

Does browsers automatically decode URL?

It is a usual task in web development, and this is generally done while making a GET request to the API with the query params. The query params must also be encoded in the URL string, where the server will decode this. Many browsers automatically encode and decode the URL and the response string.

What is decode URL?

UrlDecode(String, Encoding) Converts a URL-encoded string into a decoded string, using the specified encoding object.

What is %20 in the URL?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.


1 Answers

There is a simple way to konw if a URL string is encoded.

Take the initial string and compare it with the result of decoding it. If the result is the same, the string is not encoded; if the result is different then it is encoded.

I had this issue with my urls and I used this functions:

function isEncoded(uri) {   uri = uri || '';    return uri !== decodeURIComponent(uri); } 

So now I can use isEncoded as the discriminant in a while loop (or with recursion) to know if I need to keep calling decodeURIComponent on the string:

function fullyDecodeURI(uri){    while (isEncoded(uri)){     uri = decodeURIComponent(uri);   }    return uri; } 

This solved the problem for me of decoding urls encoded multiple times. Hope this helps.

like image 61
rodu Avatar answered Sep 21 '22 06:09

rodu