HTTP Methods Get Certified
Understanding HTTP Methods
HTTP methods (also called HTTP verbs) tell the server what action the client wants to perform on a resource. They are the foundation of REST APIs and form the CRUD operations: Create, Read, Update, Delete.
Common HTTP Methods
- GET – Retrieve data from the server (safe and idempotent).
- POST – Create a new resource or submit data.
- PUT – Replace an entire existing resource.
- PATCH – Partially update a resource.
- DELETE – Remove a resource from the server.
HTTP Methods Code Examples
1. GET Request with Fetch
The most common method. Used to read data without changing anything on the server.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GET Method Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
button { padding: 10px 20px; margin: 5px; cursor: pointer; }
#result {
margin-top: 15px;
padding: 15px;
background: white;
border: 1px solid #ddd;
min-height: 80px;
}
</style>
</head>
<body>
<h2>GET Request Demo</h2>
<button onclick="getData()">Fetch Data (GET)</button>
<div id="result">Click the button to send a GET request...</div>
<script>
function getData() {
const result = document.getElementById("result");
result.innerHTML = "Sending GET request...";
// Using a public test API
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(response => {
if (!response.ok) throw new Error("HTTP " + response.status);
return response.json();
})
.then(data => {
result.innerHTML = "<strong>Title:</strong> " + data.title +
"<br><strong>Body:</strong> " + data.body;
})
.catch(err => {
result.innerHTML = "Error: " + err.message;
});
}
</script>
</body>
</html>
2. POST Request (Create Resource)
POST is used to send data to the server and create a new resource.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>POST Method Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
input, button { padding: 8px; margin: 5px 0; display: block; width: 250px; }
button { width: auto; cursor: pointer; }
#result {
margin-top: 15px;
padding: 15px;
background: white;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<h2>POST Request Demo</h2>
<input type="text" id="title" placeholder="Post title">
<input type="text" id="body" placeholder="Post body">
<button onclick="createPost()">Create Post (POST)</button>
<div id="result">Enter data and click the button...</div>
<script>
function createPost() {
const title = document.getElementById("title").value;
const body = document.getElementById("body").value;
const result = document.getElementById("result");
result.innerHTML = "Sending POST request...";
fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
title: title,
body: body,
userId: 1
})
})
.then(response => response.json())
.then(data => {
result.innerHTML = "<strong>Created!</strong><br>" +
"ID: " + data.id + "<br>" +
"Title: " + data.title;
})
.catch(err => {
result.innerHTML = "Error: " + err.message;
});
}
</script>
</body>
</html>
3. PUT vs PATCH vs DELETE
Demonstrates the difference between full update (PUT), partial update (PATCH), and deletion.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PUT / PATCH / DELETE</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
button { padding: 10px 16px; margin: 5px; cursor: pointer; }
#result {
margin-top: 15px;
padding: 15px;
background: white;
border: 1px solid #ddd;
min-height: 60px;
}
</style>
</head>
<body>
<h2>Update & Delete Demo</h2>
<button onclick="updatePut()">PUT (Full Update)</button>
<button onclick="updatePatch()">PATCH (Partial)</button>
<button onclick="deleteItem()">DELETE</button>
<div id="result">Click a button to test the method...</div>
<script>
const result = document.getElementById("result");
const url = "https://jsonplaceholder.typicode.com/posts/1";
function updatePut() {
result.innerHTML = "Sending PUT request...";
fetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: 1,
title: "Updated Title",
body: "Completely new body content",
userId: 1
})
})
.then(r => r.json())
.then(data => {
result.innerHTML = "<strong>PUT Success</strong><br>Title: " + data.title;
})
.catch(e => result.innerHTML = "Error: " + e.message);
}
function updatePatch() {
result.innerHTML = "Sending PATCH request...";
fetch(url, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title: "Only title changed" })
})
.then(r => r.json())
.then(data => {
result.innerHTML = "<strong>PATCH Success</strong><br>Title: " + data.title;
})
.catch(e => result.innerHTML = "Error: " + e.message);
}
function deleteItem() {
result.innerHTML = "Sending DELETE request...";
fetch(url, { method: "DELETE" })
.then(r => {
if (r.ok) {
result.innerHTML = "<strong>DELETE Success</strong> – Resource removed (status " + r.status + ")";
} else {
result.innerHTML = "Delete failed: " + r.status;
}
})
.catch(e => result.innerHTML = "Error: " + e.message);
}
</script>
</body>
</html>
HTTP Methods Summary
Important Notes
- 405 Method Not Allowed – The server does not support the method you used for that endpoint.
- GET requests should never change data on the server (they are safe).
- PUT is idempotent (calling it multiple times has the same effect). POST is not.
Web Resources on HTTP Methods
1. MDN : HTTP Request Methods
2. MDN : GET method
3. MDN : POST method
4. MDN : HTTP Overview
Questions and Answers related to HTTP Methods
The five most common methods are GET (read), POST (create), PUT (full update), PATCH (partial update), and DELETE (remove).
POST = Create, GET = Read, PUT/PATCH = Update, DELETE = Delete. This mapping is the core of RESTful API design.
Use PUT when you want to replace the entire resource. Use PATCH when you only need to change one or a few fields.
405 Method Not Allowed means the server understands the request method but the target resource does not support it.
Yes. GET is defined as a safe method — it should never modify data on the server. It is also idempotent.
🧪 Test Your HTML Code
Edit the HTML code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.