I'm developing a Flask API. I want to create an API route that with accept JSON parameters and based on that json to do a search in database. My code looks like this:
@mod_api.route('/test', methods=['POST'])
def test():
query_params = json.loads(request.data)
json_resp = mongo.db.mydb.find(query_params)
return Response(response=json_util.dumps(json_resp), status=200, mimetype='application/json')
Now when I run the api i go to my route: This example looks like this:
http://0.0.0.0:5002/api/test
I don't know exactly how to send a json parameter. If i do like this:
http://0.0.0.0:5002/api/test?{'var1':'123', 'var2':'456'}
I get an error ValueError("No JSON object could be decoded")
How to send this json parameter?
request.datato see what you get. maybe you should usetest?args="{'var1':'123', 'var2':'456'}"ortest?var1=123&var2=456to get correct values inrequest.data.