You are on page 1of 7

08/10/2023

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
What's HTTP? What's HTTP?
HTTP/ . OK Content-Type: text/html; charset=UTF-!
HTTP (Hyper Text Transfer Protocol) is the protocol used when you access any website through HTTP is a message from a client to the server.
If the user clicks the hyperlink below:
your browser <!DOCTYPE html>
a href="https://www.example.com">Visit <html>
The HTTP request message is sent as: <head>
ü HTTP/1.1 and HTTP/2 are the most prevalent
GET / HTTP/1.1 Host: www.example.com <title>Welcome to Example.com</title>
versions used on the web today.
</head>
ü The choice of which version to use depends on the <body> <h >Welcome to Example.com</h >
The response includes an HTML page
web server's support and the specific <p>This is an example website.</p>
(text/html) that the browser can render.
requirements of the application or website. </body>
</html>

DIS Scholar year 2023/2024 UMBB 01 DIS Scholar year 2023/2024 UMBB 02

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
What's HTTP? How URLs work?
HTTP requests and responses are easy to read and understand.
Here is an example of what an HTTP request and response might look like:

Scheme It indicates the protocol being used, such as "http" for unencrypted HTTP or "https" for secure
HTTP. For example, "http" in "http://www.examplecat.com" or "https" in
"https://www.examplecat.com".
Domain This part specifies the server or domain where the resource is located. In the examples above,
"www.examplecat.com" is the domain or host.
Port It's an optional component that specifies the port number to use for the connection. If omitted
(optional)
(deleted), the default ports are 80 for HTTP and 443 for HTTPS.

DIS Scholar year 2023/2024 UMBB 03 DIS Scholar year 2023/2024 UMBB 04
08/10/2023

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
How URLs work? How URLs work? (examples)

Here are a few examples of HTTP URLs:


ü Simple HTTP URL: http://www.example.com
Path This part defines the specific location or path to the resource on the server. It is separated from
ü HTTP URL with Path: http://www.example.com/products/page.html
the domain by a forward slash ("/").
ü HTTP URL with Query Parameters: http://www.example.com/search?query=example
Query It is used to send parameters to the resource, typically in the form of key-value pairs. It is
(optional) ü Secure HTTPS URL with Port: https://www.example.com:8443
preceded by a question mark ("?").
ü URL with Fragment: http://www.example.com/page.html#section2
Fragment It is used to specify a particular section or anchor within the resource, typically preceded by a
(optional)
hash symbol ("#").

DIS Scholar year 2023/2024 UMBB 05 DIS Scholar year 2023/2024 UMBB 06

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Requests Requests
Anatomy of an HTTP requests Anatomy of an HTTP requests

HTTP requests typically include: An optional request body is available in

ü a domain (likeexamplecat.com) HTTP requests. Generally, HTTP GET

ü a resource (like /cat.png) requests do not include a request body,

ü a method (Get, Post, or something else) while HTTP POST requests often do

ü a header (Extra information for a server) include one

DIS Scholar year 2023/2024 UMBB 07 DIS Scholar year 2023/2024 UMBB 08
08/10/2023

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Requests Requests
Request methods Request methods
Every HTTP request has a method, which is the first element in the first line of the request.
GET The GET method is used to request data from a specified resource. It retrieves data without
causing any side effects on the server or altering the resource. GET requests are typically used
for reading information.
There are 9 standard HTTP methods. In practice, about 80% of the time, you will primarily use POST and GET
POST The POST method is used to submit data to be processed to a specified resource. It can create
new resources, update existing ones, or perform other actions that may have side effects on the
server.
PUT The PUT method is used to update or replace an existing resource at a specified URI with a new
representation.

DIS Scholar year 2023/2024 UMBB 09 DIS Scholar year 2023/2024 UMBB 10

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Requests Requests
Request methods Request methods

DELETE The DELETE method is used to request the removal of a resource at a specified URI. It instructs OPTIONS The OPTIONS method is used to request information about the communication options available
the server to delete the resource, if possible. for a resource. It can be used to determine which HTTP methods and headers are supported by
PATCH The PATCH method is used to apply partial modifications to a resource. It is typically used when a resource.
you want to update only a portion of a resource, rather than replacing the entire resource. CONNECT The CONNECT method is used to establish a network connection to a resource, typically for use
HEAD The HEAD method is similar to GET but only retrieves the headers of the response, not the actual with a proxy server.
data. It is often used to check the headers or verify the existence of a resource without TRACE TRACE: The TRACE method is used to retrieve a diagnostic trace of the current connection,
downloading its content. helping with debugging and troubleshooting.

DIS Scholar year 2023/2024 UMBB 11 DIS Scholar year 2023/2024 UMBB 12
08/10/2023

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Requests Requests
Request headers Request headers
The most crucial request headers include: The most crucial request headers include:

DIS Scholar year 2023/2024 UMBB 13 DIS Scholar year 2023/2024 UMBB 14

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Requests Requests
Request headers Using HTTP APIs (Web application programming interfaces (APIs))
The most crucial request headers include: What is an API?

Web APIs are designed to represent resources such as HTML pages, access web

services, retrieve data from databases, and integrate with third-party services (such as

social media platforms or payment gateways). They define a set of endpoints (URLs)

and the corresponding HTTP methods (GET, POST, PUT, DELETE, etc.) that

developers can use to request and exchange data with a remote server or service.

DIS Scholar year 2023/2024 UMBB 15 DIS Scholar year 2023/2024 UMBB 16
08/10/2023

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Requests Requests
Using HTTP APIs (Web application programming interfaces (APIs)) Using HTTP APIs (Web application programming interfaces (APIs))
HTTP Request Example (GET): HTTP Response Example (200 OK): HTTP/ . OK

GET /api/users/ / HTTP/ . Content-Type: application/json

Host: api.example.com {

Accept: application/json "id": /,

Authorization: Bearer YOUR_API_KEY "name": <Amin fname",


"email": " Aminfname @example.com"
In this example:
}
ü The request uses the HTTP method GET. In this response:
ü It requests the resource located at /api/users/123 on the api.example.com server. ü The status code 200 OK indicates a successful response.
ü The accept header specifies that the client prefers to receive the response in JSON format. ü The Content-Type header specifies that the response contains JSON data.
ü The Authorization header includes an API key or token for authentication. ü The response body is a JSON object with user information.
DIS Scholar year 2023/2024 UMBB 17 DIS Scholar year 2023/2024 UMBB 18

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Responses Responses
Anatomy of an HTTP response Response headers
There are a few kinds of response headers

HTTP responses typically include:

ü a status code (200 OK!, 404 not found! )

ü headers

ü a body (HTML, an image, JSON, etc )

DIS Scholar year 2023/2024 UMBB 19 DIS Scholar year 2023/2024 UMBB 20
08/10/2023

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Responses Responses
Response headers Response headers
There are a few kinds of response headers There are a few kinds of response headers

DIS Scholar year 2023/2024 UMBB 21 DIS Scholar year 2023/2024 UMBB 22

II- HyperText Transfer Protocol (HTTP) II- HyperText Transfer Protocol (HTTP)
Responses Responses
Status codes Status codes
Every HTTP response has a status code There are approximately 50 status codes, but in practice, these are the most commonly
encountered ones.

There are approximately 50 status codes, but in practice, these are the most commonly
encountered ones.

DIS Scholar year 2023/2024 UMBB 23 DIS Scholar year 2023/2024 UMBB 24
TS 02: HTTP protocol

Module: DSI Groups: Third-Year Bachelor's Degree A.Belgacem

Exercise 1:
1. What does an HTTP request typically include?
2. What are the main HTTP request methods?
3. What is a URL in an HTTP request?
4. What is the purpose of HTTP headers in a request?
5. When is a POST request typically used?
6. What does an HTTP response typically include?
7. How is a GET request different from a POST request?
8. What is an API in an HTTP request?

Exercise 2:
Write an HTTP GET request to retrieve information about a user with the name amin from the
following API: URL: https://api.example.com/users?nama=amin

Exercise 3:
Write an HTTP POST request to create a new user on the server using the following API:
API URL: https://api.example.com/users
Request Body (Json code): { "name": "Karim", "email": "karim@example.com" }

Exercise 4:
Given an HTTP response with the following status line:
HTTP/1.1 200 OK
What is the meaning of the status code, and is it a successful response?

Exercise 5:
Given an HTTP response with the following headers:
HTTP/1.1 302 Found
Location: https://www.example.com/redirected-page
Content-Type: text/html; charset=utf-8
Explain the purpose of each header in the response.

You might also like