Testing a POST request using cURL¶
Using the following Pyramid application:
1from wsgiref.simple_server import make_server
2from pyramid.view import view_config
3from pyramid.config import Configurator
4
5@view_config(route_name='theroute', renderer='json',
6 request_method='POST')
7def myview(request):
8 return {'POST': request.POST.items()}
9
10if __name__ == '__main__':
11 config = Configurator()
12 config.add_route('theroute', '/')
13 config.scan()
14 app = config.make_wsgi_app()
15 server = make_server('0.0.0.0', 6543, app)
16 print server.base_environ
17 server.serve_forever()
Once you run the above application, you can test a POST request to the
application via curl
(available on most UNIX systems).
$ python application.py
{'CONTENT_LENGTH': '', 'SERVER_NAME': 'Latitude-XT2', 'GATEWAY_INTERFACE': 'CGI/1.1',
'SCRIPT_NAME': '', 'SERVER_PORT': '6543', 'REMOTE_HOST': ''}
To access POST request body values (provided as the argument to the
-d
flag of curl
) use request.POST
.
1$ curl -i -d "param1=value1¶m2=value2" http://localhost:6543/
2HTTP/1.0 200 OK
3Date: Tue, 09 Sep 2014 09:34:27 GMT
4Server: WSGIServer/0.1 Python/2.7.5+
5Content-Type: application/json; charset=UTF-8
6Content-Length: 54
7
8{"POST": [["param1", "value1"], ["param2", "value2"]]}
To access QUERY_STRING parameters as well, use request.GET
.
1@view_config(route_name='theroute', renderer='json',
2 request_method='POST')
3def myview(request):
4 return {'GET':request.GET.items(),
5 'POST':request.POST.items()}
Append QUERY_STRING parameters to previously used URL and query with curl.
1$ curl -i -d "param1=value1¶m2=value2" http://localhost:6543/?param3=value3
2HTTP/1.0 200 OK
3Date: Tue, 09 Sep 2014 09:39:53 GMT
4Server: WSGIServer/0.1 Python/2.7.5+
5Content-Type: application/json; charset=UTF-8
6Content-Length: 85
7
8{"POST": [["param1", "value1"], ["param2", "value2"]], "GET": [["param3", "value3"]]}
Use request.params
to have access to dictionary-like object
containing both the parameters from the query string and request body.
1@view_config(route_name='theroute', renderer='json',
2 request_method='POST')
3def myview(request):
4 return {'GET':request.GET.items(),
5 'POST':request.POST.items(),
6 'PARAMS':request.params.items()}
Another request with curl.
1$ curl -i -d "param1=value1¶m2=value2" http://localhost:6543/?param3=value3
2HTTP/1.0 200 OK
3Date: Tue, 09 Sep 2014 09:53:16 GMT
4Server: WSGIServer/0.1 Python/2.7.5+
5Content-Type: application/json; charset=UTF-8
6Content-Length: 163
7
8{"POST": [["param1", "value1"], ["param2", "value2"]],
9 "PARAMS": [["param3", "value3"], ["param1", "value1"], ["param2", "value2"]],
10 "GET": [["param3", "value3"]]}
Here's a simple Python program that will do the same as the curl
command above does.
1import httplib
2import urllib
3from contextlib import closing
4
5with closing(httplib.HTTPConnection("localhost", 6543)) as conn:
6 headers = {"Content-type": "application/x-www-form-urlencoded"}
7 params = urllib.urlencode({'param1': 'value1', 'param2': 'value2'})
8 conn.request("POST", "?param3=value3", params, headers)
9 response = conn.getresponse()
10 print response.getheaders()
11 print response.read()
Running this program on a console.
$ python request.py
[('date', 'Tue, 09 Sep 2014 10:18:46 GMT'), ('content-length', '163'), ('content-type', 'application/json; charset=UTF-8'), ('server', 'WSGIServer/0.1 Python/2.7.5+')]
{"POST": [["param2", "value2"], ["param1", "value1"]], "PARAMS": [["param3", "value3"], ["param2", "value2"], ["param1", "value1"]], "GET": [["param3", "value3"]]}