[Python EVE]:读取Python EVE中的请求参数值

2024-10-03 17:16:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pythoneve开发一个web应用程序。我几乎没有暴露的端点。我有一个名为persons的端点,在db schema userid中有一个字段。我想使用读取请求参数后生成的随机字符串填充该字段。在

我的问题是如何读取这些参数。我在Eve文档中找不到相同的语法。在

这方面的任何帮助都是有帮助的


Tags: 字符串文档web应用程序db参数schema语法
2条回答

我想这样的方法会奏效的:

from eve import Eve
from flask import request

def update_inbound_docs(items):
    # retrieve request parameter, if present
    my_arg = request.args.get('key') 
    for document in items:
        # update document 'userid' field according to my_arg
        # value. replace with custom logic.
        document['userid'] = 'bingo' if my_arg else 'ack!'

app = Eve()

# bind your callback to the POST method for 'persons'
# endpoint. it will be invoked after a POST request 
# has been validated and before db is updated, so any 
# update to 'items' will be persisted.
app.on_insert_persons += update_inbound_docs

app.run()

Eve是一个Flask应用程序,因此您可以随意使用Flaskrequest对象。有关详细信息,请参见Database Hooks。在

我没有足够的声誉来评论尼古拉的回答。在

线

app_on_insert_persons += update_inbound_docs

应该是

^{pr2}$

附言: 可以使用request.args["foo"]访问请求http://example.com/?foo=bar的查询字符串

相关问题 更多 >