Request - Response
Flow
- Client sends a request (e.g. HTTP GET request)
- Server parses the request
- Server processes the request (e.g. database query or other business logic)
- Server sends a response (e.g. JSON)
- Client parses the response (e.g. parse JSON)
- Client processes the response (e.g. display data to user)
Use cases
- HTTP, DNS, SSH
- RPC (remote procedure call)
- APIs (REST, SOAP, GraphQL, etc.)
- SQL and database protocols
- many more
Structure
- Request structure is defined by a protocol (which both client and server must agree on) and a message format
HTTP GET request example:
GET /users HTTP/1.1
Host: example.com
Accept: application/json
... (other headers)
- The same is true for the response
HTTP response example:
HTTP/1.1 200 OK
Server: example.com
Content-Type: application/json
... (other headers)
<CLRF>
[body]
Doesn't work well for ...
- Real-time communication (e.g. chat applications)
- Notifications (e.g. push notifications) -> client would need to poll the server regularly
- Long-running requests (e.g. file upload) -> client would need to wait for the response (what if the connection is lost?)
Demo
Request:
curl -v google.com
Response:
* Trying 172.217.20.14:80...
* Connected to google.com (172.217.20.14) port 80 (#0)
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.81.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>