Software /
code /
prosody-modules
Diff
mod_rest/README.markdown @ 3820:d3757e089433
mod_rest: Add a JSON callback example
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Jan 2020 18:11:55 +0100 |
parent | 3813:aa1ad69c7c10 |
child | 3826:21ffca4d3aae |
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