-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathformdata2json.js
More file actions
31 lines (27 loc) · 897 Bytes
/
formdata2json.js
File metadata and controls
31 lines (27 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
// formdata2json - Transforms POST form data to JSON
//
// Example: curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST <endpoint>
module.exports = async function (context) {
let result = {};
const b = context.request.body;
if (typeof b === "object") {
// Content-type allowed express to parse formdata as object already
result = b
} else {
// We need to parse the formdata it ourselves
let data = decodeURIComponent(b.toString()).replace(/\+/g, " ");
let dataParts = data.split('&');
for (let i = 0; i < dataParts.length; i++) {
let kv = dataParts[i].split("=");
result[kv[0]] = kv[1];
}
}
return {
status: 200,
body: result,
headers: {
'Content-Type': 'application/json'
}
}
};