-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini-http-client.html
More file actions
259 lines (256 loc) · 8.88 KB
/
mini-http-client.html
File metadata and controls
259 lines (256 loc) · 8.88 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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Minimal HTTP Client</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html, body {
margin: 0;
padding: 0;
min-height: 100vh;
font-family: system-ui, sans-serif;
background: #fafbfc;
color: #1a1a1a;
}
body {
width: 100vw;
min-height: 100vh;
}
.container {
width: 98vw;
max-width: 1200px;
margin: 0 auto;
padding: 32px 18px;
border-radius: 8px;
box-shadow: 0 0 8px 0 #e5e5e5;
background: #fff;
min-height: 98vh;
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.row {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 16px;
}
label {
min-width: 10%;
font-size: 1rem;
}
select, input[type="text"], textarea {
flex: 2 1 200px;
min-width: 0;
padding: 7px 10px;
font-size: 1rem;
border: 1px solid #d6d6d6;
border-radius: 4px;
background: #fafbfc;
}
textarea {
width: 100%;
min-height: 80px;
resize: vertical;
}
.headers-list {
margin-bottom: 8px;
}
.header-row {
display: flex;
gap: 6px;
margin-bottom: 5px;
}
.header-row input[type="text"] {
flex: 2 1 80px;
}
.header-row button {
padding: 2px 9px;
border: none;
border-radius: 3px;
background: #ececec;
cursor: pointer;
}
.buttons-row {
display: flex;
justify-content: space-between;
gap: 10px;
}
button {
padding: 7px 16px;
border: none;
border-radius: 4px;
background: #222;
color: #fff;
font-size: 1rem;
cursor: pointer;
transition: background .2s;
}
button:active {
background: #333;
}
#response {
margin-top: 22px;
padding: 12px;
background: #f4f4f4;
border-radius: 6px;
min-height: 40px;
white-space: pre-wrap;
font-family: ui-monospace, "SFMono-Regular", "Consolas", monospace;
font-size: 0.97em;
overflow-x: auto;
}
@media (max-width: 900px) {
.container { max-width: 98vw; }
}
@media (max-width: 600px) {
.container { padding: 8px; min-height: unset; }
.row, .buttons-row { flex-direction: column; }
button, select, input, textarea { font-size: 1em; }
}
</style>
</head>
<body>
<div class="container">
<form id="httpForm" autocomplete="off">
<div class="row">
<label for="method">Método</label>
<select id="method" name="method">
<option>GET</option>
<option>POST</option>
<option>PUT</option>
<option>PATCH</option>
<option>DELETE</option>
<option>OPTIONS</option>
<option>HEAD</option>
</select>
</div>
<div class="row">
<label for="url">URL</label>
<input type="text" id="url" name="url" required placeholder="https://suaapi.com/endpoint">
</div>
<div class="row">
<label style="align-self: flex-start;">Headers</label>
<div style="flex: 2 1 200px;">
<div id="headersList" class="headers-list"></div>
<button type="button" id="addHeaderBtn" style="width: 100%;">Adicionar header</button>
</div>
</div>
<div class="row">
<label for="body" style="align-self: flex-start;">Body</label>
<textarea id="body" name="body" placeholder="Corpo da requisição (JSON, texto, etc.)"></textarea>
</div>
<div class="buttons-row">
<div>
<button type="button" id="importBtn">Importar Requisição</button>
<button type="button" id="exportBtn">Exportar Requisição</button>
</div>
<button type="submit" style="min-width:110px;">Enviar</button>
</div>
</form>
<div id="response"></div>
<input type="file" id="importInput" style="display:none;">
</div>
<script>
// Helpers para headers dinâmicos
const headersListDiv = document.getElementById('headersList');
function renderHeaders(headers) {
headersListDiv.innerHTML = '';
headers.forEach((h, i) => {
const row = document.createElement('div');
row.className = 'header-row';
row.innerHTML = `
<input type="text" placeholder="Chave" value="${h.key||''}">
<input type="text" placeholder="Valor" value="${h.value||''}">
<button type="button" title="Remover">✕</button>
`;
row.querySelector('button').onclick = () => {
headers.splice(i, 1);
renderHeaders(headers);
};
row.querySelectorAll('input').forEach((input, idx) => {
input.oninput = e => {
if (idx === 0) headers[i].key = input.value;
else headers[i].value = input.value;
};
});
headersListDiv.appendChild(row);
});
}
let headers = [];
renderHeaders(headers);
document.getElementById('addHeaderBtn').onclick = function () {
headers.push({key: '', value: ''});
renderHeaders(headers);
};
// Exportação
document.getElementById('exportBtn').onclick = function () {
const data = {
method: document.getElementById('method').value,
url: document.getElementById('url').value,
headers: headers.filter(h => h.key.trim()),
body: document.getElementById('body').value
};
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = "requisicao.json";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(()=>URL.revokeObjectURL(url), 150);
};
// Importação
document.getElementById('importBtn').onclick = function () {
document.getElementById('importInput').click();
};
document.getElementById('importInput').onchange = function (e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (evt) {
try {
const data = JSON.parse(evt.target.result);
document.getElementById('method').value = data.method || 'GET';
document.getElementById('url').value = data.url || '';
document.getElementById('body').value = data.body || '';
headers = Array.isArray(data.headers) ? data.headers : [];
renderHeaders(headers);
} catch { alert('Arquivo inválido!'); }
};
reader.readAsText(file);
e.target.value = '';
};
// Envio da requisição
document.getElementById('httpForm').onsubmit = async function (e) {
e.preventDefault();
const method = document.getElementById('method').value;
const url = document.getElementById('url').value.trim();
const reqHeaders = {};
headers.forEach(h => {
if (h.key.trim()) reqHeaders[h.key] = h.value;
});
let body = document.getElementById('body').value;
let fetchOptions = { method, headers: reqHeaders };
// Só envia body em métodos adequados
if (['POST', 'PUT', 'PATCH'].includes(method.toUpperCase()) && body.trim().length) {
fetchOptions.body = body;
}
document.getElementById('response').textContent = 'Enviando...';
try {
const resp = await fetch(url, fetchOptions);
let text = await resp.text();
let display = `Status: ${resp.status} ${resp.statusText}\n`;
display += `\nHeaders:\n${[...resp.headers].map(([k,v])=>`${k}: ${v}`).join('\n')}\n`;
display += `\nBody:\n${text}`;
document.getElementById('response').textContent = display;
} catch (err) {
document.getElementById('response').textContent = 'Erro: ' + err;
}
};
</script>
</body>
</html>