1

I found the solution to my (dumb) problem and listed it below.

I'm using Python 2.7.1+ on Ubuntu 11.04. The client/server are on the same computer.

From the Wing debugger, I know the server code is being called and I can walk through the code one line at a time. In this instance, I know 22 bytes were transferred.

In Firebug, I saw this data under the Net Post tab:

Parameter   application/x-www-form-urlencoded
fname   first
lname   last
Source
Content-Type: application/x-www-form-urlencoded
Content-Length: 22 fname=first&lname=last

This is the client code that I'm using:

<html>
     <form action="addGraphNotes.wsgi" method="post">
         First name: <input type="text" name="fname" /><br />
         Last name: <input type="text" name="lname" /><br />
         <input type="submit" value="Submit" />
    </form>
</html>

And this is the server code:

import urlparse 

def application(environ, start_response):
    output = []

    # the environment variable CONTENT_LENGTH may be empty or missing
    try:
    # NOTE: THIS WORKS. I get a value > 0 and the size appears correct (22 bytes in this case)
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0

    try:
        # environ['QUERY_STRING'] returns ""
        **values = urlparse.parse_qs( environ['QUERY_STRING'] )**
    except:
        output = ["parse error"]

In the Wing debugger, I've verified that data is being passed from the client to the server:

>>> environ['wsgi.input'].read()
'fname=first&lname=last'

FOUND MY PROBLEM. I COPIED AND PASTED IN THE WRONG CODE. THIS IS THE CODE I WAS USING FOR FORMS BUT STOP ADDING IT WHEN I STARTED USING AJAX AND STOPPED USING FORMS. Now, everything is working fine.

# When the method is POST the query string will be sent
# in the HTTP request body which is passed by the WSGI server
# in the file like wsgi.input environment variable.
request_body = environ['wsgi.input'].read(request_body_size)

values = parse_qs(request_body) 
Jarek
  • 65
  • 1
  • 9

1 Answers1

3

You're doing a POST query so the QUERY_STRING is indeed going to be empty as it represents the query string of a GET request (it can also appear in other request types but it's unrelated to the problem at hand). You are supposed to parse POST data by consuming the wsgi.input stream.

patrys
  • 2,729
  • 17
  • 27
  • I tried 'get' but that didn't change anything. All my other form/submit code that I wrote earlier used 'post' and the code worked fine. Something seems to be 'broken' on this machine and I can't figure out what it is. – Jarek Sep 27 '11 at 12:52
  • I've been using post in my form submits all along and everything worked just fine. Until I starting using this computer. – Jarek Sep 27 '11 at 12:54