Question

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) ?
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top