在做后端api开发的时候经常要验证和调试自己开发的接口是否生效,每次在浏览器搜索框里面调用显得非常麻烦,所以就自己用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
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GET and POST Test</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f4f4f9;
color: #333;
}
h1 {
color: #444;
}
.input-row, .input-group, .field-header {
margin-bottom: 10px;
display: flex;
align-items: center;
}
.input-row label {
margin-right: 10px;
font-weight: bold;
}
.input-row input {
flex: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
}
.input-group input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
}
.input-group .key, .input-group .value {
width: 40%;
}
.input-group button, .buttons button, .add-field-button {
padding: 8px 12px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.input-group button:hover, .buttons button:hover, .add-field-button:hover {
background-color: #0056b3;
}
.input-group button {
width: 20%;
margin-left: 10px;
background-color: #DC3545;
}
.input-group button:hover {
background-color: #c82333;
}
.buttons {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
.buttons button {
width: 48%;
}
#response {
margin-top: 20px;
padding: 10px;
background-color: #e9ecef;
border: 1px solid #ccc;
border-radius: 4px;
white-space: pre-wrap;
}
#fields {
margin-bottom: 20px;
}
.field-header {
display: flex;
align-items: center;
border-bottom: 2px solid #ccc;
padding-bottom: 5px;
margin-bottom: 10px;
}
.field-header span {
font-weight: bold;
}
.field-header .key, .field-header .value {
width: 40%;
}
.field-header .action {
width: 20%;
}
.container {
max-width: 800px;
margin: 0 auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.add-field-button {
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<h1>GET 和 POST 测试</h1>
<div class="input-row">
<label for="url">URL:</label>
<input type="text" id="url" placeholder="Enter URL">
</div>
<div id="fields">
<div class="field-header">
<span class="key">Key</span>
<span class="value">Value</span>
<span class="action">Action</span>
</div>
</div>
<button class="add-field-button" onclick="addField()">添加字段</button>
<div class="buttons">
<button onclick="sendGetRequest()">GET</button>
<button onclick="sendPostRequest()">POST</button>
</div>

<div id="response"></div>
</div>

<script>
function addField() {
const fields = document.getElementById('fields');
const inputGroup = document.createElement('div');
inputGroup.className = 'input-group';

const keyInput = document.createElement('input');
keyInput.type = 'text';
keyInput.className = 'key';
keyInput.placeholder = 'Enter Key';

const valueInput = document.createElement('input');
valueInput.type = 'text';
valueInput.className = 'value';
valueInput.placeholder = 'Enter Value';

const removeButton = document.createElement('button');
removeButton.innerText = '删除字段';
removeButton.onclick = function() {
removeField(removeButton);
};

inputGroup.appendChild(keyInput);
inputGroup.appendChild(valueInput);
inputGroup.appendChild(removeButton);
fields.appendChild(inputGroup);

// 显示字段标题行
document.querySelector('.field-header').style.display = 'flex';
}

function removeField(button) {
const inputGroup = button.parentNode;
inputGroup.parentNode.removeChild(inputGroup);

// 检查是否还有其他字段
const fieldGroups = document.querySelectorAll('.input-group');
if (fieldGroups.length === 0) {
// 隐藏字段标题行
document.querySelector('.field-header').style.display = 'none';
}
}

function sendGetRequest() {
const url = document.getElementById('url').value;
if (!url) {
alert('URL不能为空');
return;
}
const keys = document.querySelectorAll('.key');
const values = document.querySelectorAll('.value');
let params = '';
keys.forEach((key, index) => {
if (key.value && values[index].value) {
params += `${key.value}=${encodeURIComponent(values[index].value)}&`;
}
});

const xhr = new XMLHttpRequest();
xhr.open('GET', `${url}?${params.slice(0, -1)}`, true);
xhr.onload = function() {
document.getElementById('response').innerText = 'Response: ' + xhr.responseText;
};
xhr.send();
}

function sendPostRequest() {
const url = document.getElementById('url').value;
if (!url) {
alert('URL不能为空');
return;
}
const keys = document.querySelectorAll('.key');
const values = document.querySelectorAll('.value');
let params = '';
keys.forEach((key, index) => {
if (key.value && values[index].value) {
params += `${key.value}=${encodeURIComponent(values[index].value)}&`;
}
});

const xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
document.getElementById('response').innerText = 'Response: ' + xhr.responseText;
};
xhr.send(params.slice(0, -1));
}

// 添加默认字段
window.onload = function() {
addField();
};
</script>
</body>
</html>