changeset 2989:926aaaeb0d21

mod_post_msg: Add support for a JSON based format similar to what mod_component_http uses
author Kim Alvefur <zash@zash.se>
date Wed, 04 Apr 2018 15:55:09 +0200
parents 3cc78e6a8758
children cb8d65b40fb4
files mod_post_msg/README.markdown mod_post_msg/mod_post_msg.lua
diffstat 2 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_post_msg/README.markdown	Wed Apr 04 15:54:30 2018 +0200
+++ b/mod_post_msg/README.markdown	Wed Apr 04 15:55:09 2018 +0200
@@ -48,9 +48,14 @@
 `application/x-www-form-urlencoded`
 :   Allows more fields to be specified.
 
+`application/json`
+:   Similar to form data.
+
+Which one is selected via the `Content-Type` HTTP header.
+
 ### Data fields
 
-The form data format allow the following fields:
+The form data and JSON formats allow the following fields:
 
 `to`
 :   Can be used instead of having the receiver in the URL.
--- a/mod_post_msg/mod_post_msg.lua	Wed Apr 04 15:54:30 2018 +0200
+++ b/mod_post_msg/mod_post_msg.lua	Wed Apr 04 15:55:09 2018 +0200
@@ -7,6 +7,7 @@
 local b64_decode = require "util.encodings".base64.decode;
 local formdecode = require "net.http".formdecode;
 local xml = require"util.xml";
+local json = require "util.json";
 
 local function require_valid_user(f)
 	return function(event, path)
@@ -57,6 +58,11 @@
 			end
 			message:tag("html", {xmlns="http://jabber.org/protocol/xhtml-im"}):add_child(html):up();
 		end
+	elseif body_type == "application/json" then
+		local post_body = json.decode(request.body);
+		if not post_body then return 400; end
+		message = msg({ to = post_body.to or to, from = authed_user,
+		                type = post_body.type or "chat"}, post_body.body);
 	else
 		return 415;
 	end