Reputation: 3907
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?
Upvotes: 1
Views: 1891
Reputation:
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.
Upvotes: 1
Reputation: 2649
Using a regular expression, you can convert the output to underscore. This method takes advantage of the facts that:
URLRequest
's data
variable is a generic Object
String
is an Object
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);
Upvotes: 1