Python 使用 Bottle.py 读取 POST 正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14988887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading POST body with bottle.py
提问by Martín Coll
I am having trouble reading a POST request with bottle.py.
我在读取 POST 请求时遇到问题bottle.py。
The request sent has some text in its body. You can see how it's made here on line 29: https://github.com/kinetica/tries-on.js/blob/master/lib/game.js.
发送的请求在其正文中包含一些文本。您可以在第 29 行看到它是如何制作的:https: //github.com/kinetica/tries-on.js/blob/master/lib/game.js。
You can also see how it's read on a node-based client here on line 4: https://github.com/kinetica/tries-on.js/blob/master/masterClient.js.
您还可以node在第 4 行的基于 - 的客户端上查看它是如何读取的:https: //github.com/kinetica/tries-on.js/blob/master/masterClient.js。
However, I haven't been able to mimic this behavior on my bottle.py-based client. The docssay that I can read the raw body with a file-like object, but I can't get the data neither using a for loop on request.body, nor using request.body's readlinesmethod.
但是,我无法在bottle.py基于我的客户端上模仿这种行为。该文档说,我可以用一个类似文件的对象读取原始的身体,但也使用了上环,我不能得到数据request.body,也不能使用request.body的readlines方法。
I'm handling the request in a function decorated with @route('/', method='POST'), and requests arrive correctly.
我在用 装饰的函数中处理请求@route('/', method='POST'),并且请求正确到达。
Thanks in advance.
提前致谢。
EDIT:
编辑:
The complete script is:
完整的脚本是:
from bottle import route, run, request
@route('/', method='POST')
def index():
for l in request.body:
print l
print request.body.readlines()
run(host='localhost', port=8080, debug=True)
回答by Jan Vlcinsky
Did you try simple postdata = request.body.read()?
你试过简单postdata = request.body.read()吗?
Following example shows reading posted data in raw format using request.body.read()
以下示例显示使用原始格式读取发布的数据 request.body.read()
It also prints to the log file (not to the client) raw content of body.
它还打印到日志文件(而不是客户端)body 的原始内容。
To show accessing of form properties, I added returning "name" and "surname" to the client.
为了显示对表单属性的访问,我添加了向客户端返回“name”和“surname”。
For testing, I used curl client from command line:
为了测试,我从命令行使用 curl 客户端:
$ curl -X POST -F name=jan -F surname=vlcinsky http://localhost:8080
The code which works for me:
对我有用的代码:
from bottle import run, request, post
@post('/')
def index():
postdata = request.body.read()
print postdata #this goes to log file only, not to client
name = request.forms.get("name")
surname = request.forms.get("surname")
return "Hi {name} {surname}".format(name=name, surname=surname)
run(host='localhost', port=8080, debug=True)
回答by Warf
Simple script for processing POSTed data. POSTed data are written in a terminal and returned to the client:
处理 POSTed 数据的简单脚本。POSTed 数据写入终端并返回给客户端:
from bottle import get, post, run, request
import sys
@get('/api')
def hello():
return "This is api page for processing POSTed messages"
@post('/api')
def api():
print(request.body.getvalue().decode('utf-8'), file=sys.stdout)
return request.body
run(host='localhost', port=8080, debug=True)
Script for POSTing json data to the script above:
用于将 json 数据发布到上述脚本的脚本:
import requests
payload = "{\"name\":\"John\",\"age\":30,\"cars\":[ \"Ford\", \"BMW\",\"Fiat\"]}"
url = "localhost:8080/api"
headers = {
'content-type': "application/json",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

