Skip to content
Pixel
العربية

3-2Unit 3 · Web Applications

Web Application Communication Methods

11 min readStart exercises
In this lesson
  1. The Big Idea
  2. Client and Server: the Request–Response Cycle
  3. HTTP and HTTPS: the Rules of the Conversation and How to Protect It
  4. HTTP Requests and Responses: Method and Status Code
  5. Application Programming Interface (API) and JSON
  6. The Whole Picture: One Request's Journey
  7. Try It Yourself
  8. Common Mistakes
  9. Key Points
  10. FAQ

The Big Idea

In the last lesson you saw that the frontend and backend work together to carry out what the user asks for. This lesson is about how they talk. When you click a link or start a search, your device sends a request across the network to a service on a server; the server processes it and sends back a response. The side that asks is called the client. The conversation follows rules set by a protocol such as HTTP, it can be protected with HTTPS, every request carries a method that says what it wants, and every response carries a status code that says how it went.

Client and Server: the Request–Response Cycle

In many web applications, communication involves two sides with clear roles:

  • Client: a program or device that sends a request to another service, asking for data or processing. The two classic examples are a web browser and a phone app. Your banking app or a delivery app is just as much a client as a browser is.
  • Server: the side that receives requests from clients, processes them and sends back responses.

The same cycle repeats thousands of times a day: the client sends a request and the server replies with a response. One server can serve many clients at once: your laptop, your brother's phone and a tablet in another school can all send requests to the same service, and each gets its own response.

The client requests, the server responds Request-response diagram. On the left is the client, a web browser or phone app; on the right is the server, which receives the request, processes it and replies. A blue arrow from client to server is the request, carrying the line GET /schedule HTTP/1.1. A green arrow back from server to client is the response, carrying the line HTTP/1.1 200 OK. Below: HTTP sets the rules of this exchange, and every response carries a status code that reports the result. The client requests, the server responds Client Web browser Phone app Server Receives the request Processes and replies 1) Request GET /schedule HTTP/1.1 2) Response HTTP/1.1 200 OK HTTP sets the rules for this exchange, and every response carries a status code with the result
A request goes out from the client and a response comes back from the server. That is the basic cycle.

Linking this to the previous lesson

In lesson 3-1 we split a web application into a frontend, a backend and a database. The frontend runs on the client (in the browser or app) and the backend runs on the server. So when "the frontend sends the search words to the backend", that is really a request travelling from client to server, and when it "displays the results", that happens after the response arrives.

HTTP and HTTPS: the Rules of the Conversation and How to Protect It

HTTP: the agreed language

For the client and server to understand each other, they need shared rules that set the shape of each message, its order and what it carries. Those rules are called a protocol. HTTP (Hypertext Transfer Protocol) is the protocol used to exchange messages between a client and a server on the web. Every request your browser sends and every response a server returns is written according to HTTP's rules.

HTTPS: the same HTTP, inside a secure connection

On its way, a message passes through many devices and networks: café Wi-Fi, your internet provider's equipment and more. If it is sent over plain HTTP, an eavesdropper along the route may be able to read it, or even change it.

That is where HTTPS comes in. It means using HTTP over a secure TLS connection. TLS (Transport Layer Security) helps with three things:

  1. Confidentiality: an eavesdropper cannot easily read the content while it travels.
  2. Integrity: the content cannot easily be changed on the way without that being detected.
  3. Server authentication: checking that you are really talking to the right server and not a fake one pretending to be it.

Notice that HTTPS is not a different language: the messages are still HTTP messages, just travelling inside a protected "tube". You can tell a site uses HTTPS when its address starts with https://.

Plain HTTP versus HTTPS Two panels. In the first, the client sends a login message over plain HTTP and it travels as readable text, so an eavesdropper on the way can read the username and password and could change them. In the second, the same message goes over HTTPS, meaning HTTP inside a secure TLS connection drawn as a closed tube with a lock, so the eavesdropper sees scrambled symbols and cannot easily read or change them. At the bottom: TLS protects confidentiality and integrity and helps authenticate the server. 1) Plain HTTP: the message travels as is Client http:// Server user=mona&pass=1234 An eavesdropper can read it, and could change it 2) HTTPS: the same HTTP inside a secure TLS connection Client https:// Server 7f#Qz!0p@Lw2$kR TLS The eavesdropper sees scrambled data it cannot easily change TLS protects confidentiality and integrity, and helps authenticate the server
The message itself is the same; what changes is the road it travels on.

HTTP Requests and Responses: Method and Status Code

Every HTTP message carries information that helps the other side understand it. The two most important pieces in this lesson are the method in the request and the status code in the response.

HTTP request method: what do you want from the server?

The HTTP request method is information inside the request that says what kind of operation is wanted. The book focuses on two:

| Method | Usual role | Examples | |---|---|---| | GET | Usually requests a resource or data from the server | Viewing a web page, getting search results | | POST | Usually sends data for the server to process | Sending login details, submitting a form |

Think of it like this: GET means "give me", and POST means "take this data and process it". Note the word usually in the Arabic edition: this is each method's normal use, and that is what exam questions ask about.

Here is a simple GET request asking an online shop for search results for the word "dictionary":

http
GET /search?q=dictionary HTTP/1.1
Host: shop.example

The first line carries the method (GET), then the path being requested on the server, then the protocol version. The second line names the site. A login request, by contrast, uses POST, because it sends data for the server to process and check:

http
POST /login HTTP/1.1
Host: portal.school.example
Content-Type: application/x-www-form-urlencoded

user=mona&pass=1234

The data being sent (username and password) comes after a blank line, in the body of the request. Because it is sensitive, this request must travel over HTTPS, not plain HTTP.

HTTP status code: how did it go?

The response from the server includes a number that reports the result of processing the request. It is called the HTTP status code. The three in the book:

| Code | Meaning | |---|---| | 200 | Success: the request was processed successfully | | 404 | The requested resource does not exist at this address | | 500 | Internal server error that stopped the server from completing the request; the code alone does not identify the detailed cause |

Common HTTP status codes Three cards for HTTP status codes. The green card 200 means success: the request was processed successfully. The yellow card 404 means the requested resource does not exist at this address, so check the link or address. The red card 500 means an internal server error stopped the server from completing the request. A note at the bottom says the code 500 alone does not tell you the detailed cause. A status code reports the result of a request 200 Success The request was processed successfully All good 404 Not found No resource at this address Check the link or address 500 Server error An internal error blocked the request Problem is on the server Note: 500 alone does not tell you the detailed cause
One number in the response tells the client what happened to its request.

Be precise about the difference between 404 and 500:

  • With 404, the request reached the server and the server is working, but it found nothing at that address: maybe the link is mistyped, or the page was deleted or moved. The Arabic edition says "requested resource", and a resource can be a page, an image, a file or data.
  • With 500, something went wrong inside the server itself and stopped it finishing the request. The code doesn't say what exactly; finding the cause needs investigation on the server side.

This is what the first line of a response, the one carrying the code, looks like:

http
HTTP/1.1 404 Not Found
http
HTTP/1.1 500 Internal Server Error

Application Programming Interface (API) and JSON

What is an API?

Not every request comes from a person opening a page. Very often a program asks another program for data. That needs an API (Application Programming Interface): a set of clear rules that lets one program request data or functions from another program. It defines what you can ask for, where to ask, and what shape the answer will take.

Examples close to you:

  • The weather app on your phone doesn't measure the temperature itself; it sends a request to a weather service's API, which returns today's forecast.
  • An online shop's frontend may send a request to a backend service through an API and receive the product list to display.
  • One school's system might request today's timetable from another school's system through an API instead of copying it by hand.

What is JSON?

When an API returns data, it may use JSON (JavaScript Object Notation). JSON is a common text format for representing and exchanging structured data, built from three things:

  • Objects: a set of key–value pairs, written between curly braces.
  • Arrays: an ordered list of values between square brackets.
  • Simple values: such as text in double quotes, numbers, and true/false.

Because it is organised text, many programming languages and tools support it, so a server written in one language and a phone app written in another can swap data easily.

Activity from the Arabic book: read the data (JSON)

Look at this data:

json
{
  "city": "Cairo",
  "temperature": 31
}

Task: name one key and its matching value. The data is a single object with two pairs:

  • Key "city" with value "Cairo", which is text (note the quotes).
  • Key "temperature" with value 31, which is a number (no quotes).

Naming either pair is enough. The key always comes before the colon and the value after it.

Here is a slightly bigger example a weather service might return, with text, a number, a true/false value and an array:

json
{
  "city": "Alexandria",
  "temperature": 27,
  "rain": false,
  "nextDays": [26, 28, 29]
}

The value of "rain" is false (no rain), and the value of "nextDays" is an array of three temperatures for the coming days. This is what might arrive in the body of a successful response:

http
HTTP/1.1 200 OK
Content-Type: application/json

{"city": "Alexandria", "temperature": 27}

The first line carries status code 200, and the second tells the client that the body is written in JSON.

The Whole Picture: One Request's Journey

Let's put everything together. A student called Mona opens her school portal to see today's timetable:

  1. Mona types the portal address, which starts with https://, and the browser (client) connects to the server over a secure TLS connection.
  2. She logs in: the browser sends a POST request carrying her username and password inside the protected connection.
  3. The server checks the details and returns a response with status code 200.
  4. She clicks "Today's timetable": the browser sends a GET request for the timetable.
  5. The server might request the timetable from another system through an API, receive it as JSON, prepare it and send it back to the browser.
  6. If Mona clicks a link to an old page that was deleted, the server returns 404. If an internal fault happens in the server's program while preparing the timetable, it returns 500.

Try It Yourself

Try
GET or POST?
Sort each action by the HTTP method it usually uses.
Items (8)
Tap an item to select it, then tap the category it belongs to.
Nothing here yet
Nothing here yet
Try
The journey of a page request
Put the steps in order, from clicking a link to seeing the page.
1. The server returns a response with status code 200 and the page
2. The browser (client) prepares an HTTP request with the GET method
3. The browser shows the page on your screen
4. The request reaches the server over the network
5. You click the link to the news page on the school website
6. The server processes the request and prepares the page
Use the up and down buttons to move each step, then press Check.
Try
An online pharmacy
The situation
A pharmacy in Tanta has a website where customers order medicines, log in and enter their delivery address. The site's address currently starts with http:// only. The owner asks you: what is the most important change to how it communicates?
What would you do? You can try more than one choice.

Common Mistakes

"HTTPS is a completely different protocol from HTTP"

HTTPS is not a new language. The messages are still HTTP messages, with the same methods and the same status codes.

HTTPS is HTTP used over a secure TLS connection, which protects confidentiality and integrity and helps authenticate the server.

"A 404 means the server is broken"

With a 404 the server is working and has actually answered you; it just didn't find the requested resource at that address. An internal server fault has a different code.

404 = the requested resource does not exist at this address; 500 = an internal server error stopped the request being completed.

"A 500 tells me exactly what went wrong"

500 only says an internal error happened on the server. On its own it doesn't say whether it was a bug in the program or something else.

Finding the detailed cause of a 500 needs investigation on the server side, not just the code.

"GET is for sending and POST is for requesting"

That is exactly the reverse of normal use, and it is a favourite trap in true/false questions.

GET is usually used to request a resource or data (viewing a page, search results); POST is usually used to send data for the server to process (login, forms).

"An API and JSON are the same thing", or "JSON is encrypted"

An API is the set of rules letting one program request data or functions from another; JSON is just a text format the data may come in. JSON is readable text with no encryption at all.

An API may return data as JSON, and protection in transit is HTTPS's job.

Key Points

  • A client is a program or device that sends a request (such as a browser or phone app); a server receives requests, processes them and returns responses.
  • The request–response model is basic to many web applications, though other means of communication can be used when needed.
  • HTTP is the protocol for exchanging messages between client and server on the web.
  • HTTPS is HTTP over a secure TLS connection, protecting confidentiality and integrity and helping authenticate the server; such sites have addresses starting https://.
  • The HTTP request method states the kind of operation: GET usually requests a resource or data; POST usually sends data for the server to process.
  • The HTTP status code reports the result: 200 success, 404 resource not at this address, 500 internal server error whose detailed cause the code alone doesn't give.
  • An API is a set of clear rules letting one program request data or functions from another, and it may return data as JSON.
  • JSON is a common text format for structured data built from objects (key–value pairs), arrays and simple values, supported by many languages.

FAQ

What is the difference between HTTP and HTTPS in one line?

HTTP sets the rules for exchanging messages between client and server; HTTPS is the same HTTP over a secure TLS connection, protecting the messages' confidentiality and integrity in transit and helping verify the server's identity. You can spot it by an address starting with https://.

Does a phone app count as a client, like a browser?

Yes. In the Arabic edition a client is any program or device that sends a request to another service, and the book gives the web browser and the phone app as examples.

If I see a 404 error, is my internet connection the problem?

Usually not. Getting a 404 means your request arrived and the server answered, but it found no resource at that address. Check the link: it may be mistyped, or the page may have been deleted or moved.

How do I find the key and the value in JSON data?

Inside an object, each pair has the key before the colon and the value after it. In "city": "Cairo" the key is city and the value is Cairo; in "temperature": 31 the key is temperature and the value is the number 31.

Done reading? Test yourself