Tengo esta función que hace una solicitud de venta, funciona, pero ¿cómo puedo hacer que también envíe una imagen?
const myFunc = () => { async function postData() { const response = await fetch('http://127.0.0.1:8000/api/createroom', { method: 'POST', mode: 'cors', cache: 'no-cache', credentials: 'same-origin', headers: { 'Content-Type': 'Content-Type': 'application/json' }, redirect: 'follow', referrerPolicy: 'no-referrer', body: JSON.stringify({ name: document.getElementById('name').value, description: document.getElementById('desc').value, host: document.getElementById('host').value, }) }); return response.json(); } postData() }He agregado el campo
<input type="file" id="img" name="img" accept="image/*" />Luego modifiqué mi función así para acomodar la imagen pero no funciona por alguna razón
const myFunc = () => { async function postData() { const myInput = document.getElementById('img') const response = await fetch('http://127.0.0.1:8000/api/createroom', { method: 'POST', mode: 'cors', cache: 'no-cache', credentials: 'same-origin', headers: { 'Content-Type': 'multipart/form-data; boundary=—-WebKitFormBoundaryfgtsKTYLsT7PNUVD' }, redirect: 'follow', referrerPolicy: 'no-referrer', body: JSON.stringify({ name: document.getElementById('name').value, description: document.getElementById('desc').value, host: document.getElementById('host').value, roomimage: myInput.files[0] }) }); return response.json(); } postData() }¿Qué podría estar haciendo mal? Mi backend está en Django y funciona cuando lo pruebo en postman, así que no creo que mi backend sea el problema.