Mercurial > prosody-modules
comparison 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 |
parents | aa1ad69c7c10 |
children | 21ffca4d3aae |
comparison
equal
deleted
inserted
replaced
3819:1bab6f67eb5f | 3820:d3757e089433 |
---|---|
165 | 165 |
166 # Examples | 166 # Examples |
167 | 167 |
168 ## Python / Flask | 168 ## Python / Flask |
169 | 169 |
170 Simple echo bot that responds to messages: | 170 Simple echo bot that responds to messages as XML: |
171 | 171 |
172 ``` {.python} | 172 ``` {.python} |
173 from flask import Flask, Response, request | 173 from flask import Flask, Response, request |
174 import xml.etree.ElementTree as ET | 174 import xml.etree.ElementTree as ET |
175 | 175 |
194 | 194 |
195 if __name__ == "__main__": | 195 if __name__ == "__main__": |
196 app.run() | 196 app.run() |
197 ``` | 197 ``` |
198 | 198 |
199 And a JSON variant: | |
200 | |
201 ``` {.python} | |
202 from flask import Flask, Response, request, jsonify | |
203 | |
204 app = Flask("echobot") | |
205 | |
206 | |
207 @app.route("/", methods=["POST"]) | |
208 def hello(): | |
209 print(request.data) | |
210 if request.is_json: | |
211 data = request.get_json() | |
212 if data["kind"] == "message": | |
213 return jsonify({"body": "hello"}) | |
214 | |
215 return Response(status=501) | |
216 | |
217 | |
218 if __name__ == "__main__": | |
219 app.run() | |
220 ``` | |
221 | |
222 Remember to set `rest_callback_content_type = "application/json"` for | |
223 this to work. | |
224 | |
199 # Compatibility | 225 # Compatibility |
200 | 226 |
201 Requires Prosody trunk / 0.12 | 227 Requires Prosody trunk / 0.12 |