Software /
code /
prosody-modules
Diff
mod_rest/mod_rest.lua @ 4066:07ae583bc565
mod_rest: Add support for form-encoded output
This roughtly matches the input capabilities when given an form-encoded
payload.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 20 Jul 2020 21:42:11 +0200 |
parent | 4037:991090cb5d18 |
child | 4242:6a91d217acc9 |
line wrap: on
line diff
--- 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