In Module 1, you learned what APIs are and why applications need them.
In Module 2, you discovered how applications send requests and receive responses.
Now, imagine that you are using a school application. You want to view your examination result, register for a new course or update your student information.
How does the application tell the server exactly what it wants to do?
This is where HTTP methods come in. You will also learn how status codes tell applications whether their requests were successful or unsuccessful.
HTTP stands for Hypertext Transfer Protocol.
It is a set of rules that helps applications and web servers communicate over the internet.
Whenever you visit a website, your browser usually communicates with a web server using HTTP or its secure version, HTTPS.
For example, when you visit an online shopping website, your browser may send a request to the website's server to retrieve product information.
HTTP methods tell a server what an application wants to do with a particular resource.
Think about a school library.
Computer applications also perform similar activities using HTTP methods.
GET is used when an application wants to collect or view information from a server.
Imagine that you open your school's application to check your examination results.
The application can send a GET request to retrieve your result.
GET /students/101/results
This is an example of a request asking the server to return the examination results for student number 101.
Other examples:
POST is commonly used when an application wants to send information to a server, often to create something new.
Imagine that a student wants to register for a course.
The application sends the student's information to the server using a POST request.
POST /students
Example information being sent:
{
"name": "Amina",
"course": "Data Analytics",
"school": "Bright Future Academy"
}
The server can receive this information and create a new student record if the request is valid and authorised.
PUT is commonly used to replace an existing resource with an updated version.
Imagine that a school has a student's old profile information. The student changes their details and submits a complete replacement profile.
PUT /students/101
Example updated information:
{
"name": "Amina Bello",
"email": "amina@example.com",
"class": "SS2"
}
The server can use this information to replace the existing student record.
PATCH is used when an application wants to change only part of an existing record.
For example, imagine that a student changes their email address but wants to keep their name and class the same.
PATCH /students/101
Information being updated:
{
"email": "newemail@example.com"
}
Only the email address needs to be updated. The student's other information remains unchanged.
DELETE is used when an application wants to remove a resource from a server.
Imagine that a school administrator wants to remove an outdated course record.
DELETE /courses/25
This request asks the server to delete the course record with identification number 25.
Whether the record is actually deleted depends on the server's permissions and business rules.
| Method | Purpose | Simple Example |
|---|---|---|
| GET | Retrieve information | View examination results |
| POST | Create or submit information | Register a student |
| PUT | Replace existing information | Replace a student profile |
| PATCH | Update part of information | Change an email address |
| DELETE | Remove information | Delete an old course |
When you send a request to a server, the server usually returns a status code to explain what happened.
A status code is a number that tells an application whether a request was successful, needs more action or failed.
Think about submitting your homework to your teacher.
Status codes provide similar information when applications communicate with servers.
| Code Range | Meaning |
|---|---|
| 100โ199 | Informational responses |
| 200โ299 | Successful responses |
| 300โ399 | Redirection responses |
| 400โ499 | Client errors |
| 500โ599 | Server errors |
Let's understand these groups one after another.
These codes tell an application that the server has received the initial part of a request and that communication is continuing.
An example is 100 Continue.
These codes indicate that a request was successfully received and processed.
Common examples include:
These codes tell the application that additional action may be needed to complete the request, often by visiting another URL.
Examples include:
These codes indicate that something is wrong with the request or that the client cannot access the requested resource.
Common examples include:
These codes indicate that the server experienced a problem while trying to process the request.
Common examples include:
A student registers for a course and the server successfully creates the registration.
Possible status code: 201 CreatedA student requests a result that does not exist in the system.
Possible status code: 404 Not FoundA student tries to access a protected dashboard without providing valid login credentials.
Possible status code: 401 UnauthorizedA school server experiences an unexpected problem while processing an examination result request.
Possible status code: 500 Internal Server ErrorNow let's see how JavaScript can send a GET request and check the status code returned by a server.
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(response => {
console.log("Status Code:", response.status);
if (response.ok) {
return response.json();
}
throw new Error("The request failed");
})
.then(data => {
console.log("User Information:", data);
})
.catch(error => {
console.log("Error:", error.message);
});
You can try this example in your browser's developer console. Press F12, select Console and paste the code.
Imagine that you are helping a developer build a school management application.
Your job is to identify the correct HTTP method for each task.
Next, identify the most suitable status code for these situations:
Write your answers in your notebook or submit them through your CRMNuggets practical assessment.
HTTP methods can perform important actions. For example, POST can create records while DELETE can remove records.
This means developers must protect their APIs.
In this module, you have learned that:
Test your understanding by answering the following questions.
What does HTTP stand for?
Which HTTP method is commonly used to retrieve information?
Which method is commonly used to create a new record?
Which method is commonly used to update only part of a record?
What does HTTP status code 200 mean?
What does HTTP status code 404 mean?
Which status code indicates that a new resource was created successfully?
Which status code belongs to the server error group?
Which method is commonly used to replace an existing resource?
What is the purpose of an HTTP status code?
You have successfully completed Module 3 of Introduction to APIs for Data.
You now understand how applications use HTTP methods to communicate with servers and how status codes explain the results.