Comparison

mod_rest/mod_rest.lua @ 3911:064c32a5be7c

mod_rest: Support urlencoded form data (does a subset of JSON mapping)
author Kim Alvefur <zash@zash.se>
date Wed, 26 Feb 2020 20:14:14 +0100
parent 3910:49efd1323a1b
child 3915:80dffbbd056b
comparison
equal deleted inserted replaced
3910:49efd1323a1b 3911:064c32a5be7c
66 local parsed, err = json.decode(data); 66 local parsed, err = json.decode(data);
67 if not parsed then 67 if not parsed then
68 return parsed, err; 68 return parsed, err;
69 end 69 end
70 return jsonmap.json2st(parsed); 70 return jsonmap.json2st(parsed);
71 elseif mimetype == "application/x-www-form-urlencoded"then
72 local parsed = http.formdecode(data);
73 if type(parsed) == "string" then
74 return parse("text/plain", parsed);
75 end
76 for i = #parsed, 1, -1 do
77 parsed[i] = nil;
78 end
79 return jsonmap.json2st(parsed);
71 elseif mimetype == "text/plain" then 80 elseif mimetype == "text/plain" then
72 return st.message({ type = "chat" }, data); 81 return st.message({ type = "chat" }, data);
73 end 82 end
74 return nil, "unknown-payload-type"; 83 return nil, "unknown-payload-type";
75 end 84 end
76 85
77 local supported_types = { "application/xmpp+xml", "application/json" }; 86 local supported_types = { "application/xmpp+xml", "application/json", "application/x-www-form-urlencoded" };
78 87
79 local function decide_type(accept) 88 local function decide_type(accept)
80 -- assumes the accept header is sorted 89 -- assumes the accept header is sorted
81 local ret = supported_types[1]; 90 local ret = supported_types[1];
82 for i = 2, #supported_types do 91 for i = 2, #supported_types do