-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostmanToolWeb.html
More file actions
419 lines (367 loc) · 12.9 KB
/
postmanToolWeb.html
File metadata and controls
419 lines (367 loc) · 12.9 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>简易 Postman</title>
<style>
* {
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif
}
body {
margin: 0;
background: #f5f5f5
}
header {
background: #20232a;
color: #61dafb;
padding: 12px 24px;
font-size: 20px
}
.container {
padding: 20px;
max-width: 1200px;
margin: auto
}
.row {
display: flex;
gap: 8px;
margin-bottom: 14px
}
.row.column {
flex-direction: column
}
select,
input[type=text] {
padding: 6px 10px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px
}
button {
cursor: pointer;
border: none;
border-radius: 4px;
background: #61dafb;
color: #fff;
padding: 8px 14px
}
button:hover {
background: #4dbdff
}
.btn-danger {
background: #e74c3c
}
.btn-danger:hover {
background: #d62c1a
}
.btn-copy {
background: #888;
font-size: 12px;
padding: 4px 10px;
margin-left: 6px
}
.btn-copy:hover {
background: #666
}
.btn-gray {
background: #999
}
.btn-gray:hover {
background: #777
}
table {
width: 100%;
border-collapse: collapse
}
th,
td {
border: 1px solid #ddd;
padding: 6px
}
th {
background: #eee;
text-align: left
}
#bodyEditor {
height: 260px;
border: 1px solid #ccc;
border-radius: 4px;
overflow: hidden
}
.response {
display: none;
background: #fff;
padding: 18px;
border-radius: 6px;
padding-top: 5px;
}
.api-res-box {
padding-bottom: 8px;
border-bottom: 1px solid #ddd;
}
.resp-title {
font-weight: bold;
margin: 8px 0;
display: flex;
align-items: center
}
#statusLine {
color: rgb(255, 154, 3);
}
pre {
background: #272822;
color: #f8f8f2;
padding: 12px;
border-radius: 4px;
white-space: pre-wrap;
overflow-x: auto;
margin: 0
}
/* 新增加载动画样式 */
.loading-mask {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0,0, 0.5);
z-index: 9999;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #61dafb;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: translate(-50%, -50%) rotate(0deg); }
100% { transform: translate(-50%, -50%) rotate(360deg); }
}
</style>
<!-- CodeMirror 依赖 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/theme/idea.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/addon/hint/show-hint.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/addon/edit/closebrackets.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/addon/edit/matchbrackets.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/addon/hint/show-hint.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/addon/hint/javascript-hint.min.js"></script>
</head>
<body>
<header>简易 Postman</header>
<div class="loading-mask" id="loading"><div class="loading"></div></div>
<div class="container">
<!-- 请求行 -->
<div class="row">
<select id="method">
<option>GET</option>
<option>POST</option>
<option>PUT</option>
<option>PATCH</option>
<option>DELETE</option>
<option>HEAD</option>
<option>OPTIONS</option>
</select>
<input type="text" id="url" placeholder="https://api.example.com/endpoint" style="flex:1">
<button id="send">Send</button>
<button id="mock" class="btn-gray">Mock</button>
</div>
<!-- Header 编辑区 -->
<div class="row column">
<label>Headers:</label>
<table>
<thead>
<tr>
<th style="width:45%">Key</th>
<th>Value</th>
<th style="width:60px"></th>
</tr>
</thead>
<tbody id="headerBody">
<tr>
<td><input type="text" placeholder="Content-Type"></td>
<td><input type="text" placeholder="application/json"></td>
<td><button class="btn-danger" onclick="removeHeader(this)">X</button></td>
</tr>
</tbody>
</table>
<button style="width:110px;margin-top:6px" onclick="addHeader()">+ Header</button>
</div>
<!-- Content-Type选择 -->
<div class="row">
<label style="align-self:center">Content-Type:</label>
<select id="contentType" style="min-width:240px">
<option>application/json</option>
<option>application/x-www-form-urlencoded</option>
<option>multipart/form-data</option>
<option>text/plain</option>
<option>application/xml</option>
</select>
<!-- <button id="formatJson" class="btn-gray">Format JSON</button> -->
</div>
<!-- Body(CodeMirror) -->
<div class="row column">
<label>Body (raw):</label>
<div id="bodyEditor"></div>
</div>
<!-- 响应区 -->
<div class="response" id="respBox">
<h4 class="api-res-box">★ 响应结果:</h4>
<div class="resp-title" id="statusLine"></div>
<div class="resp-title">Headers
<button class="btn-copy" data-target="respHeaders">复制</button>
</div>
<pre id="respHeaders"></pre>
<div class="resp-title">Body
<button class="btn-copy" data-target="respBody">复制</button>
</div>
<pre id="respBody"></pre>
</div>
</div>
<script>
/* ===== CodeMirror 初始内容 ===== */
const initialBody = `{
"id": 1,
"name": "张三",
"email": "zhangsan@example.com"
}`;
const cm = CodeMirror(document.getElementById('bodyEditor'), {
value: initialBody, // ← 设置这里
mode: {
name: "javascript",
json: true
},
theme: "idea",
lineNumbers: true,
autoCloseBrackets: true,
matchBrackets: true,
lineWrapping: true,
extraKeys: {
"Ctrl-Space": "autocomplete"
}
});
/* 自动 / 手动格式化 */
cm.on("blur", () => tryFormatJson());
// document.getElementById('formatJson').onclick = tryFormatJson;
function tryFormatJson() {
const txt = cm.getValue().trim();
if (!txt) return;
try {
cm.setValue(JSON.stringify(JSON.parse(txt), null, 2));
} catch (e) {
// alert('不是有效 JSON:' + e.message);
console.log('不是有效 JSON:' + e.message);
}
}
/* Header 增删 */
function addHeader() {
const tr = document.createElement('tr');
tr.innerHTML = `<td><input type="text" placeholder="Key"></td>
<td><input type="text" placeholder="Value"></td>
<td><button class="btn-danger" onclick="removeHeader(this)">X</button></td>`;
document.getElementById('headerBody').appendChild(tr);
}
function removeHeader(btn) {
btn.parentNode.parentNode.remove();
}
/* 复制按钮 */
document.addEventListener('click', e => {
if (e.target.classList.contains('btn-copy')) {
navigator.clipboard.writeText(
document.getElementById(e.target.dataset.target).textContent
).then(() => {
e.target.textContent = '已复制';
setTimeout(() => e.target.textContent = '复制', 1000);
});
}
});
/* 发送请求 */
document.getElementById('send').addEventListener('click', async () => {
const loading = document.getElementById('loading');
const method = document.getElementById('method').value.trim();
const url = document.getElementById('url').value.trim();
const rawBody = cm.getValue();
const chosenCT = document.getElementById('contentType').value;
if (!url) {
alert('请输入 URL!');
return;
}
const headers = {};
document.querySelectorAll('#headerBody tr').forEach(tr => {
const k = tr.children[0].firstElementChild.value.trim();
const v = tr.children[1].firstElementChild.value.trim();
if (k) headers[k] = v;
});
if (chosenCT) headers['Content-Type'] = chosenCT;
const opt = {
method,
headers
};
if (method !== 'GET' && method !== 'HEAD' && rawBody !== '') opt.body = rawBody;
console.log(opt);
const t0 = performance.now();
let resp;
try {
loading && (loading.style.display = 'block');
resp = await fetch(url, opt);
} catch (err) {
showError(err);
return;
}
const ms = (performance.now() - t0).toFixed(0);
showResponse(await resp.text(), resp.status, resp.statusText, resp.headers, ms);
});
/* Mock */
document.getElementById('mock').addEventListener('click', () => {
const mock = {
id: 1,
name: "张三",
email: "zhangsan@example.com",
role: "admin"
};
showResponse(JSON.stringify(mock), 200, 'OK', new Headers({
'content-type': 'application/json',
'x-mock': 'true'
}), 0);
});
/* 展示 & 错误 */
function showResponse(body, status, txt, headersObj, ms) {
const respBoxEle = document.getElementById('respBox');
respBoxEle.style.display = 'block';
document.getElementById('statusLine').textContent = `${status} ${txt} — ${ms} ms`;
let h = '';
headersObj.forEach ? headersObj.forEach((v, k) => h += `${k}: ${v}\n`) : h = headersObj;
document.getElementById('respHeaders').textContent = h || '(none)';
document.getElementById('respBody').textContent = formatBody(body) || '(none)';
const loading = document.getElementById('loading');
loading && (loading.style.display = 'none');
// 将respBoxEle滚动到视口
setTimeout(() => {
respBoxEle.scrollIntoView({
behavior: 'smooth'
});
}, 1000);
}
function showError(err) {
showResponse('', 'Error', '', '', 0);
document.getElementById('statusLine').textContent = 'Error: ' + err.message;
}
function formatBody(text) {
try {
return JSON.stringify(JSON.parse(text), null, 2);
} catch {
return text;
}
}
</script>
</body>
</html>