Changeset

3861:ede3d1724dd1

mod_rest: Attempt to auto-discover data type wanted by callback
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2020 20:04:39 +0100
parents 3860:9752a6f1b9f3
children 3862:3b6b8dcff78e
files mod_rest/example/app.py mod_rest/mod_rest.lua
diffstat 2 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/example/app.py	Sat Jan 25 02:06:07 2020 +0100
+++ b/mod_rest/example/app.py	Sat Jan 25 20:04:39 2020 +0100
@@ -3,6 +3,11 @@
 app = Flask("echobot")
 
 
+@app.route("/api", methods=["OPTIONS"])
+def options():
+    return Response(status=200, headers={"accept": "application/json"})
+
+
 @app.route("/api", methods=["POST"])
 def hello():
     print(request.data)
--- a/mod_rest/mod_rest.lua	Sat Jan 25 02:06:07 2020 +0100
+++ b/mod_rest/mod_rest.lua	Sat Jan 25 20:04:39 2020 +0100
@@ -153,6 +153,15 @@
 		send_type = "application/json";
 	end
 
+	http.request(rest_url, {
+			method = "OPTIONS",
+		}, function (body, code, response)
+			if code == 200 and response.headers.accept then
+				send_type = decide_type(response.headers.accept);
+				module:log("debug", "Set 'rest_callback_content_type' = %q based on Accept header", send_type);
+			end
+		end);
+
 	local code2err = {
 		[400] = { condition = "bad-request"; type = "modify" };
 		[401] = { condition = "not-authorized"; type = "auth" };