-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.html
163 lines (147 loc) · 5.41 KB
/
index.html
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Postman JSON to form-data converter</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/build/pure-min.css" integrity="sha384-X38yfunGUhNzHpBaEBsWLO+A0HDYOQi8ufWDkZ0k9e0eXz/tH3II7uKZ9msv++Ls" crossorigin="anonymous"> <style>
/* Custom styles */
body {
margin: 0;
padding: 0;
}
.header {
background-color: #22668D;
color: #FFFADD;
text-align: center;
padding: 20px 0;
}
.footer {
background-color: #22668D;
color: #FFFADD;
text-align: center;
padding: 10px 0;
position: absolute;
bottom: 0;
width: 100%;
}
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 80vh;
}
.text-area-container {
display: flex;
justify-content: space-between;
align-items: center;
width: 80%;
}
.textarea {
width: 45%;
min-height: 65vh;
}
.separator-container {
margin: 0 20px;
display: flex;
justify-content: center;
align-items: center;
}
.link {
text-decoration: none;
color: #FFCC70;
}
.button-d {
background: #22668D;
color: #FFFADD;
}
</style>
</head>
<body>
<div class="header">
<h1>Postman { JSON } to form-data</h1>
</div>
<div class="container">
<form class="pure-form text-area-container">
<textarea class="textarea pure-input-rounded jsonValues" placeholder="Copy your raw JSON body here"></textarea>
<div class="separator-container">
<button class="button-d pure-button convert-json">Convert</button>
</div>
<textarea class="textarea pure-input-rounded code formDataValues" placeholder="Your form-data will appear here" readonly></textarea>
</form>
</div>
<div class="footer">
Copyright © 2022-2023 MIT by Dejan Cavic |
<a class="link" target="_blank" href="https://github.com/deki23/postman-json-to-form-data">Github</a>
</div>
<script>
function resolveObject(name, object) {
let stringToReturn = '';
for (const [key, value] of Object.entries(object)) {
if (value instanceof Object) {
stringToReturn += resolveObject(`${name}[${key}]`, value);
continue;
}
if (value instanceof Array) {
stringToReturn += resolveArray(`${name}[${key}]`, value);
continue;
}
stringToReturn += `${name}[${key}]:${value}\n`
}
return stringToReturn;
}
function resolveArray(name, array) {
let stringToReturn = '';
array.forEach((value, index) => {
if (value instanceof Object) {
stringToReturn += resolveObject(`${name}[${index}]`, value);
return;
}
if (value instanceof Array) {
stringToReturn += resolveArray(`${name}[${index}]`, value);
return;
}
stringToReturn += `${name}[${index}]:${value}\n`
})
return stringToReturn;
}
document.querySelector('.convert-json').addEventListener('click', (e) => {
try {
e.preventDefault();
e.target.textContent = 'Converting...';
const jsonValues = document.querySelector('.jsonValues').value;
const jsonObject = JSON.parse(jsonValues);
let formDataString = '';
for (const [key, value] of Object.entries(jsonObject)) {
if (value instanceof Object) {
console.log('Is object');
formDataString += resolveObject(key, value);
continue;
}
if (value instanceof Array) {
formDataString += resolveArray(key, value);
}
formDataString += `${key}:${value}\n`
}
document.querySelector('.formDataValues').value = formDataString;
} catch (err) {
// SyntaxError is in case JSON.parse() fails (invalid JSON)
if (err instanceof SyntaxError) {
document.querySelector('.jsonValues').setCustomValidity('Invalid JSON');
document.querySelector('.jsonValues').reportValidity()
setTimeout(() => {
document.querySelector('.jsonValues').setCustomValidity("")
}, 800)
} else {
alert('Unknown error, please create a github issue with your use case.')
}
} finally {
setTimeout(() => {
e.target.textContent = 'Convert';
}, 200)
}
})
</script>
</body>
</html>