Pycurl test to not receiving environ["QUERY_STRING"]


Hi all,

I’ve setup a domain with 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:


def application(environ, start_response):
        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)

    return json.loads(response.getvalue())

Thanks in advance.



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.


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

    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)


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


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