【问题标题】:[Python EVE]: Reading the request parameters values in Python EVE[Python EVE]:在 Python EVE 中读取请求参数值
【发布时间】:2015-04-16 14:50:14
【问题描述】:

我正在使用 python eve 开发一个 Web 应用程序。我暴露的端点很少。我有一个名为 people 的端点,数据库模式 userid 中有一个字段。我想使用读取请求参数后生成的随机字符串填充该字段。

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

这方面的任何帮助都会有所帮助

【问题讨论】:

    标签: python rest eve


    【解决方案1】:

    我想这样的事情会起作用:

    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 应用程序,因此您可以随意使用 Flask request 对象。详情请见Database Hooks

    【讨论】:

    • 我刚刚尝试了您提供的代码 sn-p。我收到以下错误“app_on_insert_persons”未定义。
    • 我尝试了您提供的解决方案,我无法修改请求参数,也无法打印请求参数。 @nicola-iarocci 请帮忙
    • 当我试图更新请求参数时,我得到 500 Internal Server Error <h1>内部服务器错误</h1> <p>服务器遇到内部错误,无法完成您的请求。要么服务器超载,要么应用程序出错。</p>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多