# HG changeset patch # User Kim Alvefur # Date 1595274131 -7200 # Node ID 07ae583bc56554f0ddb256b08c7ea58158d13969 # Parent 92152437ecfea95cd6434488f97a42b03f18f2d8 mod_rest: Add support for form-encoded output This roughtly matches the input capabilities when given an form-encoded payload. diff -r 92152437ecfe -r 07ae583bc565 mod_rest/mod_rest.lua --- a/mod_rest/mod_rest.lua Tue Jul 07 17:08:08 2020 +0100 +++ b/mod_rest/mod_rest.lua Mon Jul 20 21:42:11 2020 +0200 @@ -111,6 +111,7 @@ local supported_outputs = { "application/xmpp+xml", "application/json", + "application/x-www-form-urlencoded", }; if have_cbor then @@ -118,9 +119,27 @@ table.insert(supported_outputs, "application/cbor"); end +-- Only { string : string } can be form-encoded, discard the rest +-- (jsonmap also discards anything unknown or unsupported) +local function flatten(t) + local form = {}; + for k, v in pairs(t) do + if type(v) == "string" then + form[k] = v; + elseif type(v) == "number" then + form[k] = tostring(v); + elseif v == true then + form[k] = ""; + end + end + return form; +end + local function encode(type, s) if type == "application/json" then return json.encode(jsonmap.st2json(s)); + elseif type == "application/x-www-form-urlencoded" then + return http.formencode(flatten(jsonmap.st2json(s))); elseif type == "application/cbor" then return cbor.encode(jsonmap.st2json(s)); elseif type == "text/plain" then