JavaScript
Public
Fetch POST JSON Request
Send JSON data to a backend endpoint with fetch.
#api
#javascript
#post
#json
JavaScript
async function createNote(note) {
const response = await fetch('/api/notes', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(note)
});
if (!response.ok) {
throw new Error('Could not create note.');
}
return await response.json();
}
Notes
Always set Content-Type when sending JSON.