简体   繁体   中英

[Python EVE]: Reading the request parameters values in Python EVE

I am developing a web application using python eve. I have few endpoints exposed . I have an endpoint named persons , there is a field in the db schema userid . I want to populate that field using a random string that is generated after reading the request parameters.

My question is how can I read those parameters . I was not able to find the syntax for the same in Eve Documentation .

Any help in this regard would be helpful

I guess something like this would work:

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 is a Flask application so you can use Flask request object at will. See Database Hooks for details.

I don't have enough reputation to comment on Nicola's answer.

The line

app_on_insert_persons += update_inbound_docs

should be

app.on_insert_persons += update_inbound_docs

PS: The query string of a request http://example.com/?foo=bar can be accessed by using request.args["foo"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM