-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcurl-builder.html
More file actions
473 lines (420 loc) · 14.2 KB
/
Copy pathcurl-builder.html
File metadata and controls
473 lines (420 loc) · 14.2 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>cURL Builder</title>
<style>
:root {
color-scheme: dark;
--bg: #0b0e18;
--panel: #13172a;
--panel2: #191e32;
--input-bg: #1e2438;
--border: #2a3050;
--border-hover: #3d4a72;
--border-focus: #6b5ce7;
--text: #dde3f5;
--text-sec: #8892b0;
--text-faint: #4a5578;
--accent: #6b5ce7;
--accent-lite: #9b8af4;
--accent-bg: #1a1540;
--success: #43d98f;
--danger: #f06b75;
--cyan: #43d5f0;
--amber: #f0b843;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family:
system-ui,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
sans-serif;
background: radial-gradient(circle at top left, rgba(255, 255, 255, 0.08), transparent 20%), var(--bg);
color: var(--text);
display: flex;
justify-content: center;
padding: 20px;
}
.shell {
width: min(1140px, 100%);
background: rgba(19, 23, 42, 0.96);
border: 1px solid var(--border);
border-radius: 24px;
overflow: hidden;
box-shadow: 0 30px 80px rgba(0, 0, 0, 0.25);
}
.hero {
padding: 26px 28px 22px;
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
background: linear-gradient(140deg, rgba(255, 255, 255, 0.06), transparent 75%);
}
.hero h1 {
margin: 0 0 10px;
font-size: clamp(2rem, 2.6vw, 2.6rem);
letter-spacing: -0.03em;
}
.hero p {
margin: 0;
max-width: 72ch;
color: var(--text-sec);
line-height: 1.7;
}
.badge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 999px;
background: rgba(107, 92, 231, 0.12);
color: var(--accent);
font-size: 0.82rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.content {
padding: 24px 28px 30px;
display: grid;
gap: 18px;
}
.panel {
background: rgba(255, 255, 255, 0.03);
border: 1px solid var(--border);
border-radius: 20px;
padding: 20px;
display: grid;
gap: 18px;
}
.grid {
display: grid;
gap: 16px;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
.field {
display: grid;
gap: 8px;
}
label {
font-size: 0.85rem;
font-weight: 700;
color: var(--text-sec);
}
input,
select,
textarea,
button {
font: inherit;
}
input,
select,
textarea {
width: 100%;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel2);
color: var(--text);
padding: 12px 14px;
outline: none;
}
textarea {
min-height: 120px;
resize: vertical;
}
select {
appearance: none;
}
.subpanel {
display: grid;
gap: 12px;
}
.section-title {
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 0.88rem;
font-weight: 700;
color: var(--text-sec);
text-transform: uppercase;
letter-spacing: 0.08em;
}
.rows {
display: grid;
gap: 10px;
}
.row {
display: grid;
gap: 10px;
grid-template-columns: 1fr auto;
align-items: center;
}
.row input {
min-width: 0;
}
.row button {
border: 1px solid var(--border);
border-radius: 12px;
background: transparent;
color: var(--text);
padding: 10px 14px;
cursor: pointer;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.btn {
border: none;
border-radius: 999px;
padding: 12px 18px;
cursor: pointer;
font-weight: 700;
transition:
transform 0.15s,
background 0.15s;
}
.btn:hover {
transform: translateY(-1px);
}
.btn-primary {
background: linear-gradient(135deg, var(--accent), var(--accent-lite));
color: #fff;
}
.btn-copy {
background: #1f2848;
color: var(--text);
}
.btn-secondary {
background: rgba(255, 255, 255, 0.05);
color: var(--text);
}
.output {
border-radius: 18px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.04);
padding: 18px;
}
.output pre {
margin: 0;
white-space: pre-wrap;
word-break: break-word;
font-family: "Cascadia Code", Consolas, monospace;
font-size: 0.95rem;
line-height: 1.6;
}
@media (max-width: 760px) {
body {
padding: 16px;
}
.shell {
border-radius: 18px;
}
}
</style>
</head>
<body>
<div class="shell">
<header class="hero">
<span class="badge">Offline tool</span>
<h1>cURL Builder</h1>
<p>Construct and copy ready-to-run cURL commands for REST APIs with query params, headers, auth, body content, and file uploads — all offline.</p>
</header>
<main class="content">
<section class="panel">
<div class="grid">
<div class="field">
<label for="method">Method</label>
<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>
</div>
<div class="field" style="grid-column: span 2; min-width: 0">
<label for="url">URL</label>
<input id="url" value="https://api.example.com/users" placeholder="https://api.example.com/users" />
</div>
</div>
<div class="subpanel">
<div class="section-title">Query Parameters</div>
<div class="rows" id="query-list"></div>
<button class="btn btn-secondary" id="add-query">Add parameter</button>
</div>
<div class="subpanel">
<div class="section-title">Headers</div>
<div class="rows" id="header-list"></div>
<button class="btn btn-secondary" id="add-header">Add header</button>
</div>
<div class="subpanel">
<div class="grid">
<div class="field">
<label for="auth-type">Auth type</label>
<select id="auth-type">
<option value="none">None</option>
<option value="basic">Basic</option>
<option value="bearer">Bearer</option>
</select>
</div>
<div class="field">
<label for="auth-user">Username</label>
<input id="auth-user" placeholder="username" />
</div>
<div class="field">
<label for="auth-pass">Password</label>
<input type="password" id="auth-pass" placeholder="password" />
</div>
<div class="field">
<label for="auth-token">Token</label>
<input id="auth-token" placeholder="Bearer token" />
</div>
</div>
</div>
<div class="subpanel">
<div class="grid">
<div class="field">
<label for="body-type">Body type</label>
<select id="body-type">
<option value="none">None</option>
<option value="json">JSON</option>
<option value="form">Form Data</option>
<option value="raw">Raw Text</option>
<option value="multipart">Multipart</option>
</select>
</div>
<div class="field" style="grid-column: span 2">
<label for="body-content">Body content / raw payload</label>
<textarea id="body-content" placeholder='{"key":"value"}'></textarea>
</div>
</div>
<div class="field">
<label for="file-upload">File upload path</label>
<input id="file-upload" placeholder="/path/to/file.txt" />
<small style="color: var(--text-faint)">Use multipart uploads with @path in the command output.</small>
</div>
</div>
<div class="actions">
<button class="btn btn-primary" id="build-btn">Generate cURL</button>
<button class="btn btn-secondary" id="copy-btn">Copy command</button>
<button class="btn btn-secondary" id="reset-btn">Reset</button>
</div>
<div class="output">
<pre id="output">curl -X GET "https://api.example.com/users"</pre>
</div>
</section>
</main>
</div>
<script>
const queryList = document.getElementById("query-list");
const headerList = document.getElementById("header-list");
const output = document.getElementById("output");
function createRow(listEl, key = "", value = "") {
const row = document.createElement("div");
row.className = "row";
row.innerHTML = `
<input class="key" placeholder="Key" value="${key}">
<input class="value" placeholder="Value" value="${value}">
<button type="button" class="remove">Remove</button>`;
row.querySelector(".remove").addEventListener("click", () => row.remove());
listEl.appendChild(row);
}
function seedLists() {
createRow(queryList, "page", "1");
createRow(headerList, "Accept", "application/json");
}
function getRows(listEl) {
return Array.from(listEl.querySelectorAll(".row"))
.map((row) => ({
key: row.querySelector(".key").value.trim(),
value: row.querySelector(".value").value.trim()
}))
.filter((item) => item.key || item.value);
}
function escapeShell(value) {
return value.replace(/(["'$`\\])/g, "\\$1");
}
function buildCurl() {
const method = document.getElementById("method").value;
const url = document.getElementById("url").value.trim();
const queryRows = getRows(queryList);
const headerRows = getRows(headerList);
const authType = document.getElementById("auth-type").value;
const bodyType = document.getElementById("body-type").value;
const bodyContent = document.getElementById("body-content").value.trim();
const fileUpload = document.getElementById("file-upload").value.trim();
const authUser = document.getElementById("auth-user").value.trim();
const authPass = document.getElementById("auth-pass").value.trim();
const authToken = document.getElementById("auth-token").value.trim();
let finalUrl = url;
if (queryRows.length) {
const params = queryRows.map((row) => `${encodeURIComponent(row.key)}=${encodeURIComponent(row.value)}`).join("&");
finalUrl += (finalUrl.includes("?") ? "&" : "?") + params;
}
const parts = ["curl"];
parts.push("-X", method);
if (authType === "basic" && authUser) {
parts.push("-u", `${authUser}:${authPass}`);
} else if (authType === "bearer" && authToken) {
parts.push("-H", `Authorization: Bearer ${authToken}`);
}
headerRows.forEach(({ key, value }) => {
parts.push("-H", `${key}: ${value}`);
});
if (bodyType === "json" && bodyContent) {
parts.push("-H", "Content-Type: application/json", "--data", bodyContent);
} else if (bodyType === "form" && bodyContent) {
parts.push("--data", bodyContent);
} else if (bodyType === "raw" && bodyContent) {
parts.push("--data-raw", bodyContent);
}
if (bodyType === "multipart" && fileUpload) {
parts.push("-F", `file=@${fileUpload}`);
}
parts.push(finalUrl);
return parts.map((part) => (part.includes(" ") ? `"${part}"` : part)).join(" ");
}
document.getElementById("build-btn").addEventListener("click", () => {
output.textContent = buildCurl();
});
document.getElementById("copy-btn").addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(output.textContent);
document.getElementById("copy-btn").textContent = "Copied";
setTimeout(() => (document.getElementById("copy-btn").textContent = "Copy command"), 1200);
} catch (err) {
document.getElementById("copy-btn").textContent = "Copy failed";
}
});
document.getElementById("reset-btn").addEventListener("click", () => {
document.getElementById("method").value = "GET";
document.getElementById("url").value = "https://api.example.com/users";
document.getElementById("auth-type").value = "none";
document.getElementById("auth-user").value = "";
document.getElementById("auth-pass").value = "";
document.getElementById("auth-token").value = "";
document.getElementById("body-type").value = "none";
document.getElementById("body-content").value = "";
document.getElementById("file-upload").value = "";
queryList.innerHTML = "";
headerList.innerHTML = "";
seedLists();
output.textContent = 'curl -X GET "https://api.example.com/users"';
});
document.getElementById("add-query").addEventListener("click", () => createRow(queryList));
document.getElementById("add-header").addEventListener("click", () => createRow(headerList));
seedLists();
output.textContent = 'curl -X GET "https://api.example.com/users"';
</script>
</body>
</html>