简体   繁体   中英

Optional Parameters in Sanic Server

I want to take some optional body parameters in sanic server, just like we do in python.

Example in Python:

def func(a, b=1098):
    return a+b

print(func(2, 2))
print(func(1))

The same thing, I would like to do with sanic server request parameters. For now, I just simply use try block but would like to know a more efficient approach of it.

try:
    data = request.form['data'][0]
except Exception:
    data = 5

Thanks:)

Just like you would with any other dict object.

data = int(request.form.get("data", 5))

https://sanic.dev/en/guide/basics/request.html#body

FYI - I wrapped it in int because form data usually will be a string otherwise.

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