Software /
code /
prosody-modules
Changeset
3820:d3757e089433
mod_rest: Add a JSON callback example
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Jan 2020 18:11:55 +0100 |
parents | 3819:1bab6f67eb5f |
children | 3821:11272a3233ce |
files | mod_rest/README.markdown |
diffstat | 1 files changed, 27 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/mod_rest/README.markdown Wed Jan 01 17:45:07 2020 +0100 +++ b/mod_rest/README.markdown Wed Jan 01 18:11:55 2020 +0100 @@ -167,7 +167,7 @@ ## Python / Flask -Simple echo bot that responds to messages: +Simple echo bot that responds to messages as XML: ``` {.python} from flask import Flask, Response, request @@ -196,6 +196,32 @@ app.run() ``` +And a JSON variant: + +``` {.python} +from flask import Flask, Response, request, jsonify + +app = Flask("echobot") + + +@app.route("/", methods=["POST"]) +def hello(): + print(request.data) + if request.is_json: + data = request.get_json() + if data["kind"] == "message": + return jsonify({"body": "hello"}) + + return Response(status=501) + + +if __name__ == "__main__": + app.run() +``` + +Remember to set `rest_callback_content_type = "application/json"` for +this to work. + # Compatibility Requires Prosody trunk / 0.12