Student-friendly HTML, CSS, and JavaScript reference with examples
Use fetch() with POST to send data to an API.
async function sendMessage() {
const messageData = { text: 'Hello!' };
const response = await fetch('https://api.example.com/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(messageData)
});
const result = await response.json();
}
Required options:
method: 'POST' - tells server you’re sending data headers - tells server data is JSON body - the data to send (must use JSON.stringify())