1

Is it possible to use the underscore character in a URLVariables variable name? For instance, the following code outputs "my%5Fusername=foo" instead of "my_username=foo".

import flash.net.URLVariables;
var variables : URLVariables = new URLVariables("my_username=foo");  
trace(variables.toString());

Just as in the trace, the "%5F" shows up instead of the underscore in the request. Is there some way I can get the underscore character to show up instead?

Anonymous1
  • 3,877
  • 3
  • 28
  • 42

3 Answers3

1

Using a regular expression, you can convert the output to underscore. This method takes advantage of the facts that:

  1. URLRequest's data variable is a generic Object
  2. String is an Object
  3. output of toString() and replace() are String objects 3.

The code:

var url:String = "http://www.[yourDomain].com/test";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables("my_user_name=f_o_o");

// add some more variables:
variables.exampleSessionId = "test";
variables.example_Session_Id2 = "test2";

// set up the search expression:
var undPatrn:RegExp = /%5f/gi;

trace("Without '_': " + variables.toString());  
trace("With '_': " + variables.toString().replace(undPatrn, "_"));  
trace(variables);  

// navigate with %5f:    
request.data = variables.toString();
navigateToURL(request);

// navigate with underscore:    
request.data = variables.toString().replace(undPatrn, "_");
navigateToURL(request);
iND
  • 2,663
  • 1
  • 16
  • 36
1

Simply don't use URLVariables class, it's known to do other things wrong too. This URL RFC calls the underscore a special character and puts it in the same category as alphanumeric, saying that no encoding is needed. This RFC calls the part where you'd have the variables as "query" and allocates pchar to it, describing pchar as containing underscore character.

In practice URI containing underscore characters doesn't seem to give problem to browsers or servers, so it's just plain wrong to encode it.

EDIT: from further reading it looks more like that this is rather an undesirable behavior, then a mistake (the URI normalizer would know to revert the encoded underscore to it's original look), still, encoding underscore is the same as encoding letters of English alphabet - wasteful and stupid.

iND
  • 2,663
  • 1
  • 16
  • 36
  • 1
    I have suggested an edit to fix the the claim in the first paragraph, which was not exactly true. It is not *necessary* to encode the basic set of chars, but you certainly *can*. From the RFC you quoted, "On the other hand, characters that are not required to be encoded (including alphanumerics) may be encoded within the scheme-specific part of a URL, as long as they are not being used for a reserved purpose." – iND Jan 09 '12 at 16:07
0

trace(unescape(variables.toString()));

ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
  • If @Anonymous1 is trying to use `variables` in a URL, but just get the underscore character back, `unescape()` is incorrect. It would also convert spaces, ampersands, the percent symbol, and so on, back into ASCII. These would make the URL invalid. Since the underscore is part of the [URL specs](http://www.w3.org/Addressing/), converting it is allowed, and any conversion should be limited to just the underscore char. – iND Jan 09 '12 at 06:05
  • @iND: He is not using the other characters in his variable name, just the underscore. The OP states he wanted to trace out with underscore instead of his escaped/encoded character, I just double checked in Flash and my code above works for what he wanted. I am not setting anything to the unescape()'s output, so this is not breaking the URL. – ToddBFisher Jan 09 '12 at 15:24
  • You are making assumptions about the *rest* of the `variables` object: you are unescaping the entire `variables` object with your answer. We were only given simple, focused example, and `unescape()` is not a general solution for use in the `URLRequest`'s `data`. – iND Jan 09 '12 at 15:57
  • Fair enough, my answer might not have applied to what he was actually trying to accomplish. Good call. – ToddBFisher Jan 09 '12 at 16:10