用bottle.py读取POST正文[英] Reading POST body with bottle.py

本文是小编为大家收集整理的关于用bottle.py读取POST正文的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我很难阅读bottle.py.

发送的请求在其体内有一些文字.您可以在第29行上看到它是如何制作的: https:https:https:https:https://github.com/kinetica/tries-on.js/blob/master/lib/game.js .

您还可以在第4行上的基于node的客户端上读取它是如何读取的: https://github.com/kinetica/tries-on.js/blob/master/master/masterclient.js .

但是,我无法在基于bottle.py的客户端模仿这种行为. 文档 - 类似对象,但是我既不能使用request.body上的循环,也无法使用request.body's readlines方法.

我正在用@route('/', method='POST')装饰的功能来处理请求,并且请求正确到达.

预先感谢.


编辑:

完整的脚本是:

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)

推荐答案

您是否尝试过简单postdata = request.body.read()?

以下示例显示使用request.body.read()

的读取以原始格式发布的数据

它还将其打印到日志文件(不转到客户端)的原始内容.

要显示表单属性的访问,我向客户端添加了返回的"名称"和"姓".

用于测试,我从命令行使用了curl客户端:

$ curl -X POST -F name=jan -F surname=vlcinsky http://localhost:8080

对我有用的代码:

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)

其他推荐答案

简单的脚本用于处理已发布的数据.张贴的数据写在终端中并返回给客户:

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)

脚本用于将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)

本文地址:https://www.itbaoku.cn/post/1760511.html

问题描述

I am having trouble reading a POST request with 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.

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.

However, I haven't been able to mimic this behavior on my bottle.py-based client. The docs say 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 readlines method.

I'm handling the request in a function decorated with @route('/', method='POST'), and requests arrive correctly.

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)

推荐答案

Did you try simple postdata = request.body.read() ?

Following example shows reading posted data in raw format using request.body.read()

It also prints to the log file (not to the client) raw content of body.

To show accessing of form properties, I added returning "name" and "surname" to the client.

For testing, I used curl client from command line:

$ 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)

其他推荐答案

Simple script for processing POSTed data. POSTed data are written in a terminal and returned to the client:

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:

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)