| 1 |
<!doctype html> |
| 2 |
<html> |
| 3 |
|
| 4 |
<head> |
| 5 |
<title>Axios - Multipart Form Example</title> |
| 6 |
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> |
| 7 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> |
| 8 |
<style> |
| 9 |
:root { |
| 10 |
--primary: #6c63ff; |
| 11 |
--primary-dark: #5a52d5; |
| 12 |
--primary-light: #8b85ff; |
| 13 |
--secondary: #ff6584; |
| 14 |
--light: #f8f9fa; |
| 15 |
--dark: #1a1a2e; |
| 16 |
--gray: #6c757d; |
| 17 |
--gray-light: #e9ecef; |
| 18 |
--gray-dark: #495057; |
| 19 |
--success: #4bb543; |
| 20 |
--danger: #dc3545; |
| 21 |
--border-radius: 12px; |
| 22 |
--shadow: 0 8px 24px rgba(0, 0, 0, 0.12); |
| 23 |
--transition: all 0.3s ease; |
| 24 |
--card-bg: #ffffff; |
| 25 |
--text-color: #333333; |
| 26 |
--input-bg: #ffffff; |
| 27 |
--border-color: #e1e5e9; |
| 28 |
} |
| 29 |
|
| 30 |
[data-theme="dark"] { |
| 31 |
--primary: #8b85ff; |
| 32 |
--primary-dark: #6c63ff; |
| 33 |
--primary-light: #a9a4ff; |
| 34 |
--secondary: #ff6584; |
| 35 |
--light: #1a1a2e; |
| 36 |
--dark: #f8f9fa; |
| 37 |
--gray: #a0a0a0; |
| 38 |
--gray-light: #2d3748; |
| 39 |
--gray-dark: #e2e8f0; |
| 40 |
--success: #4bb543; |
| 41 |
--danger: #dc3545; |
| 42 |
--card-bg: #16213e; |
| 43 |
--text-color: #e2e8f0; |
| 44 |
--input-bg: #1a1a2e; |
| 45 |
--border-color: #2d3748; |
| 46 |
} |
| 47 |
|
| 48 |
body { |
| 49 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); |
| 50 |
padding: 20px; |
| 51 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
| 52 |
color: var(--text-color); |
| 53 |
transition: var(--transition); |
| 54 |
} |
| 55 |
|
| 56 |
[data-theme="dark"] body { |
| 57 |
background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%); |
| 58 |
} |
| 59 |
|
| 60 |
.container { |
| 61 |
max-width: 1200px; |
| 62 |
background: var(--card-bg); |
| 63 |
border-radius: var(--border-radius); |
| 64 |
box-shadow: var(--shadow); |
| 65 |
padding: 0; |
| 66 |
overflow: hidden; |
| 67 |
transition: var(--transition); |
| 68 |
} |
| 69 |
|
| 70 |
.header { |
| 71 |
background: linear-gradient(135deg, var(--primary), var(--secondary)); |
| 72 |
color: white; |
| 73 |
padding: 25px 30px; |
| 74 |
display: flex; |
| 75 |
justify-content: space-between; |
| 76 |
align-items: center; |
| 77 |
position: relative; |
| 78 |
} |
| 79 |
|
| 80 |
h1 { |
| 81 |
font-size: 28px; |
| 82 |
font-weight: 600; |
| 83 |
margin: 0; |
| 84 |
} |
| 85 |
|
| 86 |
.theme-toggle { |
| 87 |
background: rgba(255, 255, 255, 0.2); |
| 88 |
border: none; |
| 89 |
border-radius: 50px; |
| 90 |
width: 60px; |
| 91 |
height: 30px; |
| 92 |
display: flex; |
| 93 |
align-items: center; |
| 94 |
padding: 0 5px; |
| 95 |
cursor: pointer; |
| 96 |
position: relative; |
| 97 |
transition: var(--transition); |
| 98 |
} |
| 99 |
|
| 100 |
.theme-toggle:hover { |
| 101 |
background: rgba(255, 255, 255, 0.3); |
| 102 |
} |
| 103 |
|
| 104 |
.toggle-circle { |
| 105 |
width: 22px; |
| 106 |
height: 22px; |
| 107 |
background: white; |
| 108 |
border-radius: 50%; |
| 109 |
position: absolute; |
| 110 |
left: 4px; |
| 111 |
transition: var(--transition); |
| 112 |
} |
| 113 |
|
| 114 |
[data-theme="dark"] .toggle-circle { |
| 115 |
left: 34px; |
| 116 |
} |
| 117 |
|
| 118 |
.theme-toggle i { |
| 119 |
color: white; |
| 120 |
font-size: 14px; |
| 121 |
position: absolute; |
| 122 |
} |
| 123 |
|
| 124 |
.fa-sun { |
| 125 |
left: 8px; |
| 126 |
} |
| 127 |
|
| 128 |
.fa-moon { |
| 129 |
right: 8px; |
| 130 |
} |
| 131 |
|
| 132 |
.content-wrapper { |
| 133 |
display: flex; |
| 134 |
flex-direction: row; |
| 135 |
min-height: 600px; |
| 136 |
} |
| 137 |
|
| 138 |
@media (max-width: 992px) { |
| 139 |
.content-wrapper { |
| 140 |
flex-direction: column; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
.form-section { |
| 145 |
flex: 1; |
| 146 |
padding: 30px; |
| 147 |
border-right: 1px solid var(--border-color); |
| 148 |
} |
| 149 |
|
| 150 |
.result-section { |
| 151 |
flex: 1; |
| 152 |
padding: 30px; |
| 153 |
background-color: rgba(108, 99, 255, 0.03); |
| 154 |
display: flex; |
| 155 |
flex-direction: column; |
| 156 |
} |
| 157 |
|
| 158 |
[data-theme="dark"] .result-section { |
| 159 |
background-color: rgba(26, 26, 46, 0.5); |
| 160 |
} |
| 161 |
|
| 162 |
.form-group { |
| 163 |
margin-bottom: 24px; |
| 164 |
} |
| 165 |
|
| 166 |
label { |
| 167 |
display: block; |
| 168 |
margin-bottom: 8px; |
| 169 |
font-weight: 500; |
| 170 |
color: var(--text-color); |
| 171 |
} |
| 172 |
|
| 173 |
input[type="text"], |
| 174 |
input[type="number"], |
| 175 |
input[type="file"] { |
| 176 |
width: 100%; |
| 177 |
padding: 14px 16px; |
| 178 |
border: 1px solid var(--border-color); |
| 179 |
border-radius: var(--border-radius); |
| 180 |
font-size: 16px; |
| 181 |
transition: var(--transition); |
| 182 |
background-color: var(--input-bg); |
| 183 |
color: var(--text-color); |
| 184 |
} |
| 185 |
|
| 186 |
input[type="text"]:focus, |
| 187 |
input[type="number"]:focus, |
| 188 |
input[type="file"]:focus { |
| 189 |
outline: none; |
| 190 |
border-color: var(--primary); |
| 191 |
box-shadow: 0 0 0 3px rgba(108, 99, 255, 0.2); |
| 192 |
} |
| 193 |
|
| 194 |
.radio-group { |
| 195 |
display: flex; |
| 196 |
flex-direction: column; |
| 197 |
gap: 15px; |
| 198 |
margin: 25px 0; |
| 199 |
} |
| 200 |
|
| 201 |
.radio-option { |
| 202 |
display: flex; |
| 203 |
align-items: center; |
| 204 |
padding: 15px; |
| 205 |
border: 1px solid var(--border-color); |
| 206 |
border-radius: var(--border-radius); |
| 207 |
cursor: pointer; |
| 208 |
transition: var(--transition); |
| 209 |
background-color: var(--input-bg); |
| 210 |
} |
| 211 |
|
| 212 |
.radio-option:hover { |
| 213 |
background-color: rgba(108, 99, 255, 0.05); |
| 214 |
border-color: var(--primary); |
| 215 |
} |
| 216 |
|
| 217 |
.radio-option input { |
| 218 |
margin-right: 12px; |
| 219 |
transform: scale(1.2); |
| 220 |
} |
| 221 |
|
| 222 |
.radio-option label { |
| 223 |
margin-bottom: 0; |
| 224 |
cursor: pointer; |
| 225 |
} |
| 226 |
|
| 227 |
.btn-primary { |
| 228 |
background: linear-gradient(135deg, var(--primary), var(--primary-dark)); |
| 229 |
border: none; |
| 230 |
padding: 16px 28px; |
| 231 |
font-size: 16px; |
| 232 |
font-weight: 600; |
| 233 |
border-radius: var(--border-radius); |
| 234 |
cursor: pointer; |
| 235 |
transition: var(--transition); |
| 236 |
width: 100%; |
| 237 |
display: flex; |
| 238 |
align-items: center; |
| 239 |
justify-content: center; |
| 240 |
gap: 10px; |
| 241 |
color: white; |
| 242 |
box-shadow: 0 4px 12px rgba(108, 99, 255, 0.3); |
| 243 |
} |
| 244 |
|
| 245 |
.btn-primary:hover { |
| 246 |
transform: translateY(-2px); |
| 247 |
box-shadow: 0 6px 15px rgba(108, 99, 255, 0.4); |
| 248 |
} |
| 249 |
|
| 250 |
.postSubmission { |
| 251 |
margin-top: 20px; |
| 252 |
padding: 25px; |
| 253 |
background-color: var(--light); |
| 254 |
border-radius: var(--border-radius); |
| 255 |
flex-grow: 1; |
| 256 |
display: flex; |
| 257 |
flex-direction: column; |
| 258 |
} |
| 259 |
|
| 260 |
[data-theme="dark"] .postSubmission { |
| 261 |
background-color: var(--gray-light); |
| 262 |
} |
| 263 |
|
| 264 |
.checkNetwork { |
| 265 |
margin-bottom: 20px; |
| 266 |
padding: 15px; |
| 267 |
background-color: rgba(108, 99, 255, 0.1); |
| 268 |
border-left: 4px solid var(--primary); |
| 269 |
border-radius: 4px; |
| 270 |
} |
| 271 |
|
| 272 |
.text-strong { |
| 273 |
font-weight: 700; |
| 274 |
color: var(--primary); |
| 275 |
} |
| 276 |
|
| 277 |
.hidden { |
| 278 |
display: none; |
| 279 |
} |
| 280 |
|
| 281 |
.text-danger { |
| 282 |
padding: 15px; |
| 283 |
background-color: rgba(220, 53, 69, 0.1); |
| 284 |
border-left: 4px solid var(--danger); |
| 285 |
border-radius: 4px; |
| 286 |
color: var(--danger); |
| 287 |
margin-top: 20px; |
| 288 |
} |
| 289 |
|
| 290 |
.text-success { |
| 291 |
padding: 15px; |
| 292 |
background-color: rgba(75, 181, 67, 0.1); |
| 293 |
border-left: 4px solid var(--success); |
| 294 |
border-radius: 4px; |
| 295 |
color: var(--success); |
| 296 |
margin-top: 20px; |
| 297 |
} |
| 298 |
|
| 299 |
.info-box { |
| 300 |
display: flex; |
| 301 |
align-items: center; |
| 302 |
gap: 12px; |
| 303 |
margin-top: 20px; |
| 304 |
padding: 15px; |
| 305 |
background-color: rgba(108, 99, 255, 0.05); |
| 306 |
border-radius: var(--border-radius); |
| 307 |
} |
| 308 |
|
| 309 |
.info-box i { |
| 310 |
color: var(--primary); |
| 311 |
font-size: 20px; |
| 312 |
} |
| 313 |
|
| 314 |
.result-title { |
| 315 |
font-size: 20px; |
| 316 |
font-weight: 600; |
| 317 |
margin-bottom: 20px; |
| 318 |
color: var(--primary); |
| 319 |
display: flex; |
| 320 |
align-items: center; |
| 321 |
gap: 10px; |
| 322 |
} |
| 323 |
|
| 324 |
.result-content { |
| 325 |
background-color: var(--input-bg); |
| 326 |
border-radius: var(--border-radius); |
| 327 |
padding: 20px; |
| 328 |
flex-grow: 1; |
| 329 |
overflow-y: auto; |
| 330 |
border: 1px solid var(--border-color); |
| 331 |
font-family: 'Courier New', monospace; |
| 332 |
white-space: pre-wrap; |
| 333 |
} |
| 334 |
|
| 335 |
.result-placeholder { |
| 336 |
color: var(--gray); |
| 337 |
font-style: italic; |
| 338 |
text-align: center; |
| 339 |
margin-top: 50%; |
| 340 |
transform: translateY(-50%); |
| 341 |
} |
| 342 |
|
| 343 |
@media (max-width: 600px) { |
| 344 |
.container { |
| 345 |
margin: 10px; |
| 346 |
} |
| 347 |
|
| 348 |
.form-section, |
| 349 |
.result-section { |
| 350 |
padding: 20px; |
| 351 |
} |
| 352 |
|
| 353 |
.header { |
| 354 |
padding: 20px; |
| 355 |
flex-direction: column; |
| 356 |
gap: 15px; |
| 357 |
} |
| 358 |
|
| 359 |
h1 { |
| 360 |
font-size: 24px; |
| 361 |
} |
| 362 |
} |
| 363 |
</style> |
| 364 |
</head> |
| 365 |
|
| 366 |
<body> |
| 367 |
<div class="container"> |
| 368 |
<div class="header"> |
| 369 |
<h1><i class="fas fa-file-upload"></i> Multipart Form Data</h1> |
| 370 |
<button class="theme-toggle" id="themeToggle"> |
| 371 |
<i class="fas fa-sun"></i> |
| 372 |
<i class="fas fa-moon"></i> |
| 373 |
<span class="toggle-circle"></span> |
| 374 |
</button> |
| 375 |
</div> |
| 376 |
|
| 377 |
<div class="content-wrapper"> |
| 378 |
<div class="form-section"> |
| 379 |
<form role="form" class="form" onsubmit="return false;"> |
| 380 |
<div class="form-group"> |
| 381 |
<label for="someString">someString</label> |
| 382 |
<input id="someString" type="text" placeholder="Enter text value"> |
| 383 |
</div> |
| 384 |
<div class="form-group"> |
| 385 |
<label for="someNumber">someNumber</label> |
| 386 |
<input id="someNumber" type="number" placeholder="Enter numeric value"> |
| 387 |
</div> |
| 388 |
<div class="form-group"> |
| 389 |
<label for="someSmallFile">someSmallFile</label> |
| 390 |
<input id="someSmallFile" type="file"> |
| 391 |
</div> |
| 392 |
<div class="form-group"> |
| 393 |
<label for="nested.someString">nested.someString</label> |
| 394 |
<input id="nested.someString" type="text" placeholder="Enter nested text value"> |
| 395 |
</div> |
| 396 |
|
| 397 |
<div class="radio-group"> |
| 398 |
<div class="radio-option"> |
| 399 |
<input type="radio" name="format" value="formData" id="formData" checked> |
| 400 |
<label for="formData">Pass to Axios as FormData</label> |
| 401 |
</div> |
| 402 |
<div class="radio-option"> |
| 403 |
<input type="radio" name="format" value="json" id="json"> |
| 404 |
<label for="json">Pass to Axios as JSON. Let Axios convert to form data</label> |
| 405 |
</div> |
| 406 |
</div> |
| 407 |
|
| 408 |
<button id="post" type="button" class="btn btn-primary"> |
| 409 |
<i class="fas fa-paper-plane"></i> POST Data |
| 410 |
</button> |
| 411 |
</form> |
| 412 |
|
| 413 |
<div class="postSubmission"> |
| 414 |
<div id="checkNetwork" class="checkNetwork hidden"> |
| 415 |
<span class="text-strong">Check devtools to see details of request sent, and content of FormData. |
| 416 |
</span><br /> |
| 417 |
In Chromium check: devtools -> network tab -> request to "server" -> payload tab |
| 418 |
</div> |
| 419 |
|
| 420 |
<div id="errorOutput" class="text-danger hidden"> |
| 421 |
</div> |
| 422 |
</div> |
| 423 |
</div> |
| 424 |
|
| 425 |
<div class="result-section"> |
| 426 |
<div class="result-title"> |
| 427 |
<i class="fas fa-code"></i> Response Data |
| 428 |
</div> |
| 429 |
<div class="result-content" id="resultContent"> |
| 430 |
<div class="result-placeholder">Submit the form to see the response data here</div> |
| 431 |
</div> |
| 432 |
</div> |
| 433 |
</div> |
| 434 |
|
| 435 |
<div class="info-box"> |
| 436 |
<i class="fas fa-info-circle"></i> |
| 437 |
<div>This form demonstrates how to submit multipart/form-data with both text fields and files.</div> |
| 438 |
</div> |
| 439 |
</div> |
| 440 |
|
| 441 |
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> |
| 442 |
<script> |
| 443 |
(function () { |
| 444 |
// Theme toggle functionality |
| 445 |
const themeToggle = document.getElementById('themeToggle'); |
| 446 |
const body = document.body; |
| 447 |
|
| 448 |
// Check for saved theme preference or default to light |
| 449 |
const currentTheme = localStorage.getItem('theme') || 'light'; |
| 450 |
body.setAttribute('data-theme', currentTheme); |
| 451 |
|
| 452 |
themeToggle.addEventListener('click', () => { |
| 453 |
const currentTheme = body.getAttribute('data-theme'); |
| 454 |
const newTheme = currentTheme === 'light' ? 'dark' : 'light'; |
| 455 |
|
| 456 |
body.setAttribute('data-theme', newTheme); |
| 457 |
localStorage.setItem('theme', newTheme); |
| 458 |
}); |
| 459 |
|
| 460 |
// Form submission functionality |
| 461 |
document.getElementById('post').onclick = function () { |
| 462 |
// Show network/loading indicator |
| 463 |
document.getElementById("checkNetwork").classList.remove("hidden"); |
| 464 |
document.getElementById("errorOutput").classList.add("hidden"); |
| 465 |
|
| 466 |
// Grab values from inputs |
| 467 |
const someString = document.getElementById('someString').value; |
| 468 |
const someNumber = document.getElementById('someNumber').valueAsNumber; |
| 469 |
const files = Array.from(document.getElementById('someSmallFile').files); |
| 470 |
const nestedString = document.getElementById('nested.someString').value; |
| 471 |
const passAsFormData = document.querySelector('input[name="format"]:checked').value === "formData"; |
| 472 |
|
| 473 |
const data = passAsFormData ? new FormData() : {}; |
| 474 |
|
| 475 |
// Helper to add value to FormData or plain object |
| 476 |
function addValueToData(key, val, isValDefined) { |
| 477 |
if (!isValDefined) return; |
| 478 |
|
| 479 |
if (passAsFormData) { |
| 480 |
data.append(key, val); |
| 481 |
} else { |
| 482 |
const keys = key.split("."); |
| 483 |
if (keys.length === 1) { |
| 484 |
data[key] = val; |
| 485 |
} else { |
| 486 |
// Preserve existing nested objects if any |
| 487 |
data[keys[0]] = data[keys[0]] || {}; |
| 488 |
data[keys[0]][keys[1]] = val; |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
addValueToData("someString", someString, someString.length > 0); |
| 494 |
addValueToData("someNumber", someNumber, !isNaN(someNumber)); |
| 495 |
if (files.length) addValueToData("someSmallFile", files[0], true); |
| 496 |
addValueToData("nested.someString", nestedString, nestedString !== undefined && nestedString !== null); |
| 497 |
|
| 498 |
const errorOutput = document.getElementById("errorOutput"); |
| 499 |
const resultContent = document.getElementById("resultContent"); |
| 500 |
|
| 501 |
axios({ |
| 502 |
url: 'https://httpbin.org/post', // Using a test endpoint |
| 503 |
data: data, |
| 504 |
method: "post", |
| 505 |
headers: passAsFormData ? { "Content-Type": "multipart/form-data" } : {}, |
| 506 |
}) |
| 507 |
.then(res => { |
| 508 |
errorOutput.textContent = "Successfully posted!"; |
| 509 |
errorOutput.classList.remove("text-danger"); |
| 510 |
errorOutput.classList.add("text-success"); |
| 511 |
errorOutput.classList.remove("hidden"); |
| 512 |
|
| 513 |
// Display the response in the result section |
| 514 |
resultContent.textContent = JSON.stringify(res.data, null, 2); |
| 515 |
}) |
| 516 |
.catch(err => { |
| 517 |
console.error("Error posting data:", err); |
| 518 |
errorOutput.textContent = `${err.message}`; |
| 519 |
errorOutput.classList.add("text-danger"); |
| 520 |
errorOutput.classList.remove("text-success"); |
| 521 |
errorOutput.classList.remove("hidden"); |
| 522 |
|
| 523 |
// Display error in the result section |
| 524 |
resultContent.textContent = `Error: ${err.message}\n\n${JSON.stringify(err.response?.data || {}, null, 2)}`; |
| 525 |
}); |
| 526 |
}; |
| 527 |
})(); |
| 528 |
</script> |
| 529 |
</body> |
| 530 |
|
| 531 |
</html> |