Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to APIs for Data ยท Lesson 4 of 9

Module Three

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
4
Module 3 - HTTP Methods and Status Codes

MODULE 3: Understanding HTTP Methods and Status Codes

Welcome to Module 3!

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.

3.1 What Is HTTP?

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.

Remember: HTTP provides rules for communication between web applications and servers. HTTPS adds encryption to help protect that communication.

3.2 What Are HTTP Methods?

HTTP methods tell a server what an application wants to do with a particular resource.

Think about a school library.

  • You may want to view a book.
  • You may want to add a new book.
  • You may want to update a book's information.
  • You may want to remove an old book record.

Computer applications also perform similar activities using HTTP methods.

3.3 The Five Common HTTP Methods

1. GET โ€“ Retrieve Information

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:

  • Viewing available courses.
  • Checking product prices.
  • Viewing a customer's profile.
  • Checking weather information.

2. POST โ€“ Send or Create Information

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.

3. PUT โ€“ Replace Existing Information

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.

4. PATCH โ€“ Update Part of Existing Information

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.

5. DELETE โ€“ Remove Information

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.

3.4 Comparing the Five HTTP Methods

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
Easy way to remember: GET = Read, POST = Create, PUT = Replace, PATCH = Partially update, DELETE = Remove.

3.5 What Are HTTP Status Codes?

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.

  • Your teacher receives it successfully.
  • Your teacher asks you to log in again.
  • Your teacher cannot find the assignment.
  • Your teacher cannot accept the submission because of an error.

Status codes provide similar information when applications communicate with servers.

3.6 Understanding the Five Main Status Code Groups

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.

1. Informational Responses (100โ€“199)

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.

2. Successful Responses (200โ€“299)

These codes indicate that a request was successfully received and processed.

Common examples include:

  • 200 OK: The request was successful.
  • 201 Created: A new resource was successfully created.
  • 204 No Content: The request succeeded, but there is no response content to return.

3. Redirection Responses (300โ€“399)

These codes tell the application that additional action may be needed to complete the request, often by visiting another URL.

Examples include:

  • 301 Moved Permanently: The resource has a new permanent address.
  • 302 Found: The resource is temporarily available at another address.

4. Client Errors (400โ€“499)

These codes indicate that something is wrong with the request or that the client cannot access the requested resource.

Common examples include:

  • 400 Bad Request: The request contains incorrect information.
  • 401 Unauthorized: Authentication is required or has failed.
  • 403 Forbidden: Access is not allowed.
  • 404 Not Found: The requested resource cannot be found.
  • 429 Too Many Requests: Too many requests were sent within a given period.

5. Server Errors (500โ€“599)

These codes indicate that the server experienced a problem while trying to process the request.

Common examples include:

  • 500 Internal Server Error: Something unexpected went wrong on the server.
  • 502 Bad Gateway: A server received an invalid response from another server.
  • 503 Service Unavailable: The server is temporarily unable to handle the request.
  • 504 Gateway Timeout: A server did not receive a timely response from another server.

3.7 Real-Life Examples of HTTP Status Codes

Example 1: Successful Course Registration

A student registers for a course and the server successfully creates the registration.

Possible status code: 201 Created

Example 2: Missing Examination Result

A student requests a result that does not exist in the system.

Possible status code: 404 Not Found

Example 3: Login Required

A student tries to access a protected dashboard without providing valid login credentials.

Possible status code: 401 Unauthorized

Example 4: Server Problem

A school server experiences an unexpected problem while processing an examination result request.

Possible status code: 500 Internal Server Error

3.8 Practical Coding Example: Making a GET Request

Now 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);
    });

Understanding the Code

  • fetch(): Sends a request to the API.
  • response.status: Shows the HTTP status code.
  • response.ok: Checks whether the status code indicates success (200โ€“299).
  • response.json(): Converts the response body into JavaScript data.
  • catch(): Handles errors in the request or processing.

You can try this example in your browser's developer console. Press F12, select Console and paste the code.

3.9 Practical Lab: Identify HTTP Methods and Status Codes

Lab Title: Become an API Detective

Imagine that you are helping a developer build a school management application.

Your job is to identify the correct HTTP method for each task.

  1. A student wants to view examination results.
  2. A new student wants to register.
  3. A student wants to change only their email address.
  4. An administrator wants to replace a student's full profile.
  5. An administrator wants to remove an old course record.

Next, identify the most suitable status code for these situations:

  1. A request is successfully completed.
  2. A new course is successfully created.
  3. A requested student record does not exist.
  4. A user is not allowed to access a page.
  5. The server experiences an unexpected problem.

Write your answers in your notebook or submit them through your CRMNuggets practical assessment.

3.10 API Security: Be Careful With Requests

HTTP methods can perform important actions. For example, POST can create records while DELETE can remove records.

This means developers must protect their APIs.

  • Require proper authentication for protected resources.
  • Check whether a user has permission to perform an action.
  • Validate information before saving it.
  • Use HTTPS to protect information during transmission.
  • Keep private API keys secret.
  • Apply limits to prevent excessive requests.
Safety reminder: Never use DELETE or other methods to change someone else's information without permission. Practise only with test systems and accounts you are authorised to use.

3.11 Module Summary

In this module, you have learned that:

  • HTTP is a communication protocol used by web applications.
  • HTTP methods tell servers what action an application wants to perform.
  • GET retrieves information.
  • POST commonly creates or submits information.
  • PUT replaces an existing resource.
  • PATCH updates part of an existing resource.
  • DELETE removes a resource.
  • HTTP status codes explain the result of a request.
  • 200โ€“299 represents successful responses.
  • 400โ€“499 represents client errors.
  • 500โ€“599 represents server errors.
Key takeaway: HTTP methods tell a server what an application wants to do, while HTTP status codes tell the application what happened after the request was processed.

MODULE 3: KNOWLEDGE ASSESSMENT

Test your understanding by answering the following questions.

Question 1

What does HTTP stand for?

  • A. Hypertext Transfer Protocol
  • B. High Technology Transfer Program
  • C. Hyperlink Text Processing
  • D. High Transfer Technology Protocol

Question 2

Which HTTP method is commonly used to retrieve information?

  • A. POST
  • B. GET
  • C. DELETE
  • D. PATCH

Question 3

Which method is commonly used to create a new record?

  • A. GET
  • B. DELETE
  • C. POST
  • D. PATCH

Question 4

Which method is commonly used to update only part of a record?

  • A. PATCH
  • B. GET
  • C. DELETE
  • D. POST

Question 5

What does HTTP status code 200 mean?

  • A. The server is broken.
  • B. The request was successful.
  • C. The page does not exist.
  • D. The user is blocked.

Question 6

What does HTTP status code 404 mean?

  • A. Successful request
  • B. Server unavailable
  • C. Resource not found
  • D. New record created

Question 7

Which status code indicates that a new resource was created successfully?

  • A. 404
  • B. 500
  • C. 201
  • D. 403

Question 8

Which status code belongs to the server error group?

  • A. 200
  • B. 301
  • C. 404
  • D. 500

Question 9

Which method is commonly used to replace an existing resource?

  • A. PUT
  • B. GET
  • C. POST
  • D. DELETE

Question 10

What is the purpose of an HTTP status code?

  • A. To increase internet speed
  • B. To explain the result of a request
  • C. To create a website design
  • D. To store computer passwords

Congratulations!

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.

Coming Up in Module 4: Introduction to JSON and XML Data Formats. You will learn how applications organise, read and exchange information using two popular data formats.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’