Pycurl test to passenger_wsgi.py not receiving environ["QUERY_STRING"]


#1

Hi all,

I’ve setup a domain with passenger_wsgi.py to serve a REST API and from browser a GET works fine, but when I try to consume it from my laptop using pycurl, passeneger application environ[“QUERY_STRING”] is empty.

Any Ideas?

This is My code:

File: passenger_wsgi.py

def application(environ, start_response):
    try:
        kwargs = {}
        if "QUERY_STRING" in environ and environ["QUERY_STRING"] != "":
            query_string = environ["QUERY_STRING"].replace("%20", " ").replace("%22", '"').replace("%27", "'")
            kwargs = json.loads(query_string)

        os.environ["DAMMY_CONTEXT"] = context
        man = Manager.instance()
        result = man.request(**kwargs)
        start_response(200, [('Content-type', 'text/plain')])
        return [json.dumps(result, separators=(",", ":"))]
    except Exception as e:
        exception_string = traceback.format_exc()
        start_response(404, [('Content-type', 'text/plain')])
        return [json.dumps({"status": 404, "message": "Uncaught Error", "result": exception_string, "error_args": e.args}, separators=(",", ":"))]

My pycurl test:

import pycurl
import pprint
import json

from StringIO import StringIO


def test_request(url, request_dict):
    curl = pycurl.Curl()
    curl.setopt(pycurl.SSL_VERIFYHOST, 0)
    curl.setopt(pycurl.SSL_VERIFYPEER, 0)
    curl.setopt(pycurl.USERAGENT, 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101')
    curl.setopt(pycurl.URL, url)
    curl.setopt(pycurl.POST, 1)
    curl.setopt(pycurl.POSTFIELDS, json.dumps(request_dict))
    response = StringIO()
    curl.setopt(pycurl.WRITEFUNCTION, response.write)
    curl.perform()
    curl.close()

    return json.loads(response.getvalue())

Thanks in advance.

Edu


#2

Curl’s POSTFIELDS option puts the data in the HTTP POST’s content body, so it won’t appear in the QUERY_STRING. To get the the content body, read from WSGI’s environ.input environ['wsgi.input'] stream.


#3

Thanks man for pointing me to the right direction. I finally reached the POST data this way:

try:
    request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except ValueError:
    request_body_size = 0

if request_body_size == 0:
    return [json.dumps({"status": 400, "message": "Missing Parameters: [request]"}, separators=(",", ":"))]

request_body = environ['wsgi.input'].read(request_body_size)

#4

Oops, sorry about the typo in my reply (corrected), but you found the solution. Cheers.


#6

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.