API that just receives and returns bool type ʻis_hoge` in request
hoge.py
from flask import Flask, request
app = Flask(__name__)
@app.route("/", methods=['GET'])
def hello():
is_hoge = request.args.get('is_hoge', type=bool)
return f'{ is_hoge }'
if __name__ == "__main__":
app.run()
--If you send true
, True
--If you send false
, False
--If you send 0
, False
--If you send true
--If you send false
--If you send 0
What do you mean? As a result of all, True
is returned.
As a consideration of newcomer paper, I think that all of them were regarded as character strings and True
was returned.
It seems that python has a convenient function called strtobool ()
, and I will use this.
converter.py
from distutils.util import strtobool
def convert_to_bool(target_object, default_val):
if target_object is None:
return default_val
try:
return strtobool(target_object)
except:
return default_val
When the value brought in as an argument is None
or when an exception occurs, default_val
is returned.
hoge.py
is_hoge = converter.convert_to_bool(request.args.get('is_hoge'), False)
--If you send true
--If you send false
--If you send 0
0s and 1s are now returned. If it is a judgment of the truth value of a numerical value, 0 is False, and other than 0 is True, so I think there is no problem!
This is my first post, so I would appreciate it if you could let me know if there are any points that cannot be reached.
Recommended Posts