mod_rest: Attempt to auto-discover data type wanted by callback
authorKim Alvefur <zash@zash.se>
Sat, 25 Jan 2020 20:04:39 +0100
changeset 3865 ede3d1724dd1
parent 3864 9752a6f1b9f3
child 3866 3b6b8dcff78e
mod_rest: Attempt to auto-discover data type wanted by callback
mod_rest/example/app.py
mod_rest/mod_rest.lua
--- 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" };