How to use fetch to send data (POST Request)
🔹 What it means:
fetch() can not only get data but also send data to a server — for example when:
submitting a form,
creating an account,
logging in,
posting a comment.
This is done with a POST request.
🔸 Example in JavaScript:
fetch("https:api.example.com/users",
method: "POST",
headers:
"Content-Type": "application/json"
body: JSON.stringify(
name: "Anna",
age: 25
.then(response = response.json())
.then(data = console.log("Server response:", data))
.catch(error = console.error("Something went wrong:", error));
🔹 Step-by-step:
We tell fetch where to send the request.
We specify method: "POST".
We add headers (to tell the server we’re sending JSON).
We put our data inside body as JSON text.
We wait for the server to respond.
🔹 Example response:
"status": "success",
"message": "User Anna added successfully!"
💡 In short:
Term Meaning
fetch() Sends or gets data from a server
method: "POST" Used when sending data
headers Describe the type of data sent
body The actual data you’re sending
JSON.stringify() Turns an object into text for sending