How do I send data from client (javascript) to my server (python fastapi)? I am having trouble understanding the format that I need my data in. I have this “simple” example that I’ve been trying to get working to help me understand what is happening.
document.getElementById('submitButton').addEventListener('click', async (event) => {
event.preventDefault();
const formData = new FormData();
formData.append('input_data', 'I am working');
try {
const response = await fetch('/function/', {
method: 'POST',
body: formData
});
const result = await response.json();
if (response.ok) {
console.log('Response ok');
} else {
console.log('Response not ok');
}
} catch (error) {
console.log('error');
}
});
@app.post('/function/')
async def function(input_data: str = Form(...)):
print(input_data)
return {'message': 'Success!'}