1

I am trying to pass a parameter to a python cgi script, this parameter contains the plus operator.

 /cgi-bin/test.py?input=print%20"%20"%20"%20%20-+-\"

The python cgi script is simple:

#!/usr/bin/python -w
import cgi
fs = cgi.FieldStorage()

print "Content-type: text/plain\n"
strE=fs.getvalue("input")
print strE

my output is:

 print " " "  - -\"

I don't understand why the '+' plus operator is substituted by space, How may i pass the '+' plus operator?

EDIT

@Tom Anderson, answered my question, and i want to extend my question a bit more.

I have a java-script function that invokes gets the url with the parameters:

            <script type="text/javascript">

            function PostContentEditable(editableId,targetOutput)
            {
                        var texthtml = document.getElementById(editableId).innerHTML
                        var tmp = document.createElement("DIV");
                        tmp.innerHTML = texthtml ;
                        var str= tmp.textContent||tmp.innerText;

                        var xmlhttp;
                        if (str.length == 0){
                            document.getElementById(targetOutput).innerHTML = "";
                            return;
                        }
                        if(window.XMLHttpRequest){
                            xmlhttp=new XMLHttpRequest();
                        }
                        else{
                            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        xmlhttp.onreadystatechange=function(){
                            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                                 document.getElementById(targetOutput).innerHTML=xmlhttp.responseText;
                            }
                        }

                        xmlhttp.open("GET","../cgi-bin/exercise.py?input="+str,true);
                        xmlhttp.send();
            }
            </script>

Is there an automatic built-in function that replaces all special characters to what i need?

  str = escapeStringToNativeUrl(str) ?
Michael
  • 2,827
  • 4
  • 30
  • 47

1 Answers1

5

In the query part of the URL, + is a special code that means a space.

This is because the part of the HTML specification concerning forms specifies that form data is encoded as application/x-www-form-urlencoded in a query string. In that encoding, space characters are replaced by `+'.

So, Python is correctly decoding your input.

If you want to pass an actual plus, you will need to percent-encode it as %2B.

In JavaScript, i believe the right way to build the query string is with encodeURIComponent.

Community
  • 1
  • 1
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • Thanks, Great answer, where i may find the shortcuts for those special characters? – Michael Jul 28 '12 at 08:22
  • I have edited my question, is there any function in java-script that transfers my string to the correct url? – Michael Jul 28 '12 at 08:33
  • 1
    Space is the only one that gets special treatment. For everything else, if it's an [unreserved character](http://tools.ietf.org/html/rfc3986#section-2.3) you can either use it as it is, and if not, it needs to be percent-encoded. – Tom Anderson Jul 28 '12 at 08:38
  • I'm not a JS expert, but someone else has already answered that, so i've added a link to his answer. – Tom Anderson Jul 28 '12 at 08:41