使用curl测试post请求

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 (在大多数UNIX系统上可用)。

$ python application.py
{'CONTENT_LENGTH': '', 'SERVER_NAME': 'Latitude-XT2', 'GATEWAY_INTERFACE': 'CGI/1.1',
 'SCRIPT_NAME': '', 'SERVER_PORT': '6543', 'REMOTE_HOST': ''}

访问请求后的主体值(作为参数提供给 -dcurl 使用 request.POST .

1$ curl -i -d "param1=value1&param2=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"]]}

要访问查询字符串参数,请使用 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()}

将查询字符串参数附加到以前使用的URL和curl查询。

1$ curl -i -d "param1=value1&param2=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"]]}

使用 request.params 访问类似字典的对象,该对象包含来自查询字符串和请求正文的参数。

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&param2=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"]]}

下面是一个简单的python程序,它将与 curl 上面的命令有。

 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"]]}