Skip to content

HTTP Requests

Denver James Duran edited this page Oct 10, 2023 · 1 revision

HTTP Requests - DEEP Learning

Introduction

In this lesson, we'll explore HTTP requests, which are a fundamental concept in web development. They are the means by which clients (such as web browsers) communicate with servers to retrieve or send data over the internet.

Exploring the Relationship Between API and HTTP Requests

What's the relation between API and HTTP requests?

In our analogy, envision the API as the waiter in a restaurant, and HTTP requests as the orders placed by customers (clients). Just as customers interact with the waiter (API) to convey their requests, web clients interact with the API by making HTTP requests.

In the realm of web development, an API exposes a single endpoint, while it is the user's role to provide the specific requests that the API can accommodate. As developers, we design and implement these processes that the API can handle effectively.

API as a middleman between client and server

Understanding HTTP Methods

HTTP methods are the means by which a web server communicates with a client. There are five common methods that can be used to communicate with a web server:

GET Requests
The GET method is used to retrieve data from a web server. It is primarily used for reading information and should not have side effects on the server. For example, fetching a webpage or requesting data from an API. The GET method is optimized such that any same GET request should always provide the same results. The GET request should have no body, and any details should be in the URL.

**Key Characteristics of GET:**

*   **Read-Only:** GET is a read-only method that is used for retrieving data without altering the server's state. It's ideal for fetching information.
*   **Idempotent:** GET requests are idempotent, meaning that multiple identical GET requests will produce the same result. This property makes GET suitable for caching.
*   **No Request Body:** Unlike some other methods, GET requests do not include a request body. Instead, the details are typically included in the URL as query parameters.
POST Requests
POST is used to send data to a web server to create a new resource. It can be used for submitting forms, uploading files, or making any request that results in the creation of a new entity on the server.

**Key Characteristics of POST:**

*   **Create Resource:** POST requests are used to create new resources on the server. They are typically associated with actions that modify server data, such as adding a new user to a database.
*   **Non-Idempotent:** Unlike GET requests, POST requests are not idempotent. Sending the same POST request multiple times may result in the creation of multiple resources.
*   **Request Body:** POST requests include a request body where data to be added or modified is sent. This data can be in various formats, including JSON, form data, or file uploads.
PUT Requests
PUT is used to update an existing resource on the web server. It replaces the entire resource with the new data provided in the request. It's suitable for updating or replacing an entire resource.
PATCH Requests
PATCH is used to apply partial modifications to a resource. It's often used when you want to update specific fields of an existing resource without affecting the rest of it.
DELETE Requests
DELETE is used to request the removal of a resource from the web server. It is used to delete the specified resource, and it should only perform that action.

Brain Teaser

Situation : You are creating a login page, and you want to get user data from the server using their username and password.

Question : What kind of HTTP method should you use?

Answer

POST

Expand Your Knowledge: As we can see above, GET requests are used to reading data on the server, during login, we only read the data of the account owner, right? but why should we use POST instead of GET?

Quick Links

$$Dedication ❖ Empowerment $$

$$ Education ❖ Programming $$

DEEP, guided by the principles of Modularized Learning, Project-Based Education, and Industry Relevance, revolutionizes education. Our approach breaks down complex topics into manageable units, promoting lifelong learning for all. Through project-based learning, individuals gain practical skills and problem-solving abilities, ensuring they're ready to tackle real-world challenges. Industry relevance ensures that our education translates directly into valuable, career-ready skills, benefitting not only individuals but also society as a whole.

Coming Soon

Author Notes

  • Follow me on Github, Here's my GitHub username: DDuran19

Clone this wiki locally