The examples on this page are generated by the app’s tests with Spring REST Docs,
so they always match the code (see GameControllerDocsTest).
Source: github.com/randomvlad/TicTacToe.
Overview
The app renders HTML server-side with Thymeleaf, so these endpoints are browser routes rather than a JSON API.
-
Base URL: all paths are under the
/tictactoecontext path, e.g.http://localhost:8080/tictactoe/. -
Authentication: every endpoint except
/loginand the static assets (/css/*,/images/*,/js/*) requires a logged-in user. Requests without a session are redirected to/login. Log in withrickormortyand passwordpickle. -
CSRF: every
POSTmust include a_csrfform parameter that matches the session. Thymeleaf adds it as a hidden field to the app’s forms. -
Post/Redirect/Get: the
POSTgame endpoints change state and then redirect toGET /, so refreshing the page never resubmits a move.
Game
Show Game Page
GET / renders the user’s last game. If the user has no game yet, a new one is created with the player moving first.
The response is the HTML game page.
Example Request
$ curl 'http://localhost:8080/tictactoe/' -i -X GET \
--cookie 'JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A'
GET /tictactoe/ HTTP/1.1
Host: localhost:8080
Cookie: JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A
Start New Game
POST /new-game starts a new game and replaces the user’s previous one. If the computer moves first, it always takes
the center tile 1-1.
Form Parameters
| Parameter | Description |
|---|---|
|
CSRF token. Thymeleaf adds it to the app’s forms as a hidden field, so copy it from the game page. |
|
|
Example Request
$ curl 'http://localhost:8080/tictactoe/new-game' -i -X POST \
--cookie 'JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A' \
-d 'player-go-first=true&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi'
POST /tictactoe/new-game HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080
Content-Length: 59
Cookie: JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A
player-go-first=true&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi
Example Response
| Name | Description |
|---|---|
|
The game page, which renders the new game. |
HTTP/1.1 302 Found
Content-Language: en
Location: /tictactoe/
Mark Tile
POST /mark-tile marks a tile for the player, then immediately makes a random move for the computer. Nothing changes
if the game is already over or the tile ID is malformed. The response is still a redirect to the game page.
Form Parameters
| Parameter | Description |
|---|---|
|
CSRF token. Thymeleaf adds it to the app’s forms as a hidden field, so copy it from the game page. |
|
Tile to mark, as |
Example Request
$ curl 'http://localhost:8080/tictactoe/mark-tile' -i -X POST \
--cookie 'JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A' \
-d 'tile-id=1-1&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi'
POST /tictactoe/mark-tile HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080
Content-Length: 50
Cookie: JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A
tile-id=1-1&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi
Example Response
| Name | Description |
|---|---|
|
The game page, which renders the updated board. |
HTTP/1.1 302 Found
Content-Language: en
Location: /tictactoe/
User
These endpoints are provided by Spring Security.
Log In
-
GET /loginrenders the login page. -
POST /loginauthenticates the user with form parametersusername,password,_csrfand the optionalremember-me(onkeeps the user logged in across browser sessions). On success it redirects to the page that was originally requested, or to the game page. On failure it redirects to/login?error.
Log Out
POST /logout (with _csrf) ends the session, deletes the JSESSIONID cookie and redirects to /login?logout.