HTTP and Web API Overview
CS – Advanced Programming Concepts
Chess Server Review
Clients and Server Diagram
Web API
Test Driver
(JUnit tests)
Web Site
Web Browser
(Test Page)
Client App
Server
Clients
Chess Server Diagram
Model
User
AuthToken
Game
…
Register Handler
Login Handler
Join Game Handler
Data Access
UserDao
AuthTokenDao
GameDao
…
DB
User table
AuthToken table
Game table
…
Server
Main server class
Request/
Result
RegisterRequest
RegisterResult
LoginRequest
LoginResult
…
… more handlers
login
User Service
RegisterResult register(RegisterRequest)
LoginResult login(LoginRequestion)
…
… more services
Encodes objects in JSON format
Decodes objects from JSON format
Object Encoder/Decoder
register
Game Service
ListGamesResult listGames(ListGamesRequest)
CreateGameResult createGame(CreateGameRequest)
JoinGameRequest joinGame(JoinGameRequest)
…
joinGame
Contains lambdas that map
Incoming requests to handlers
HTTP Overview
How HTTP Works
HTTP Request Diagram
Server
Client App
Web
HTTP Request
HTTP Response
Client connects to Server
Server
Client App
bytes
bytes
Client connects to Server (cont. 1)
Server
Client App
213.7.98.52
128.187.80.20
www.google.com
Client connects to Server (cont. 2)
Client App
213.7.98.52
128.187.80.20
www.google.com
Server
Program D
(port 1919)
Program A
(port 80)
Program B
(port 25)
Program C
(port 10000)
Program E
(port 7777)
HTTP GET Requests
Browsing the Web with HTTP �(GET Request)
Protocol
Domain Name
Port Number
Path
GET /images/branding/googlelogo/1x/googlelogo_color_272x92dp.png HTTP/1.1
Accept: image/png,image/gif,image/jpg
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 …
Method (i.e., request type)
URL Path
HTTP Version
Headers
Browsing the Web with HTTP �(GET Response)
HTTP/1.1 200 OK
Content-Type: image/png
Content-Length: 5969
Last-Modified: Tue, 22 Oct 2019 18:30:00 GMT
PNG Image Bytes …
Empty Line (\n)
Status Code
Reason Phrase
Headers
Response Body
HTTP Version
Web API Call with HTTP (GET Request) – Get “event” request
GET /event/12345 HTTP/1.1
Authorization: 12ab34cd56ef
Method (i.e., request type)
URL Path
HTTP Version
Auth Token Header
Web API Call with HTTP (GET Response)
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 8423
JSON String Containing Event Data …
Empty Line (\n)
Status Code
Reason Phrase
Headers
Response Body
HTTP Version
Ticket to Ride Web API GET example
{ "game-list": [
{ "name": "the game", "player-count": 3 },
{ "name": "work game", "player-count": 4 },
{ "name": "church game", "player-count": 2 }
]
}
15
HTTP POST Requests
Browsing the Web with HTTP �(POST Request)
POST /search HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: text/html,text/xhtml
Accept-Encoding: gzip, deflate
Content-Length: 19
keywords=byu&cs&240
Method (i.e., request type)
URL Path
HTTP Version
Headers
Empty Line (\n)
Request Body
Browsing the Web with HTTP �(POST Response)
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 10986
�HTML Code Containing Search Results …
Empty Line (\n)
Status Code
Reason Phrase
Headers
Response Body
HTTP Version
Web API Call with HTTP (POST Request)
POST /login HTTP/1.1
Content-Type: application/json
Content-Length: 58
JSON String Containing User Name and Password …
Method (i.e., request type)
URL Path
HTTP Version
Headers
Empty Line (\n)
Request Body
Web API Call with HTTP �(POST Response)
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 972
JSON String Containing Login Result …
Empty Line (\n)
Status Code
Reason Phrase
Headers
Response Body
HTTP Version
Ticket to Ride Web API Post Example
{ "route": "atlanta-miami" }
21
HTTP Methods
(request types)
HTTP Methods
Citations
Diagrams created by Ken Rodham and Jerod Wilkerson