# HG changeset patch # User Kim Alvefur # Date 1522850109 -7200 # Node ID 926aaaeb0d21930351e3d7c2943cf7daa1002ee3 # Parent 3cc78e6a87589cbb88f154d2e0ba7b1ea0b482cc mod_post_msg: Add support for a JSON based format similar to what mod_component_http uses diff -r 3cc78e6a8758 -r 926aaaeb0d21 mod_post_msg/README.markdown --- 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. diff -r 3cc78e6a8758 -r 926aaaeb0d21 mod_post_msg/mod_post_msg.lua --- 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