使用curl测试post请求

Using the following Pyramid application:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from wsgiref.simple_server import make_server
from pyramid.view import view_config
from pyramid.config import Configurator

@view_config(route_name='theroute', renderer='json',
             request_method='POST')
def myview(request):
    return {'POST': request.POST.items()}

if __name__ == '__main__':
    config = Configurator()
    config.add_route('theroute', '/')
    config.scan()
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 6543, app)
    print server.base_environ
    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 .

$ curl -i -d "param1=value1&param2=value2" http://localhost:6543/
HTTP/1.0 200 OK
Date: Tue, 09 Sep 2014 09:34:27 GMT
Server: WSGIServer/0.1 Python/2.7.5+
Content-Type: application/json; charset=UTF-8
Content-Length: 54

{"POST": [["param1", "value1"], ["param2", "value2"]]}

要访问查询字符串参数,请使用 request.GET .

@view_config(route_name='theroute', renderer='json',
             request_method='POST')
def myview(request):
    return {'GET':request.GET.items(),
            'POST':request.POST.items()}

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

$ curl -i -d "param1=value1&param2=value2" http://localhost:6543/?param3=value3
HTTP/1.0 200 OK
Date: Tue, 09 Sep 2014 09:39:53 GMT
Server: WSGIServer/0.1 Python/2.7.5+
Content-Type: application/json; charset=UTF-8
Content-Length: 85

{"POST": [["param1", "value1"], ["param2", "value2"]], "GET": [["param3", "value3"]]}

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

@view_config(route_name='theroute', renderer='json',
             request_method='POST')
def myview(request):
    return {'GET':request.GET.items(),
            'POST':request.POST.items(),
            'PARAMS':request.params.items()}

Another request with curl.

$ curl -i -d "param1=value1&param2=value2" http://localhost:6543/?param3=value3
HTTP/1.0 200 OK
Date: Tue, 09 Sep 2014 09:53:16 GMT
Server: WSGIServer/0.1 Python/2.7.5+
Content-Type: application/json; charset=UTF-8
Content-Length: 163

{"POST": [["param1", "value1"], ["param2", "value2"]],
 "PARAMS": [["param3", "value3"], ["param1", "value1"], ["param2", "value2"]],
 "GET": [["param3", "value3"]]}

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

import httplib
import urllib
from contextlib import closing

with closing(httplib.HTTPConnection("localhost", 6543)) as conn:
    headers = {"Content-type": "application/x-www-form-urlencoded"}
    params = urllib.urlencode({'param1': 'value1', 'param2': 'value2'})
    conn.request("POST", "?param3=value3", params, headers)
    response = conn.getresponse()
    print response.getheaders()
    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"]]}