7. De server van RdvMedecins, een blackbox
De voorbeelden 17 tot en met 20, gevolgd door de casus, communiceren met een server: de JSON-server van de applicatie [RdvMedecins]. Dit is de [NestJS]-server uit de vorige cursus, waarvan de controllers geen HTML-pagina’s meer genereren maar JSON-antwoorden produceren. De bedrijfslaag en de gegevenstoegangslaag blijven ongewijzigd.
In deze cursus is deze server een blackbox: we hoeven niet te weten hoe hij is geschreven. Het enige wat telt, is de API, dat wil zeggen de lijst met HTTP-verzoeken die de server accepteert en de antwoorden die hij teruggeeft. Dit is de gebruikelijke situatie voor een clientontwikkelaar: de API is een contract en het serverteam kan achter dat contract alles wijzigen wat het wil. De broncode wordt meegeleverd (map [rdvmedecins-nestjs-json]) en is uitvoerig van commentaar voorzien, voor de nieuwsgierige lezer.
Dit hoofdstuk legt uit hoe u de server installeert, beschrijft de API en bevraagt de server met [curl].
7.1. De applicatie RdvMedecins
De applicatie beheert afspraken voor een medische praktijk. Ze kent drie rollen:
Rol | Wie | Wat ze kunnen doen |
[ADMIN] | De beheerder ([admin]) | Afspraken inplannen en annuleren voor alle patiënten; artsen en cliënten beheren |
[DOCTOR] | Artsen ([pelissier], [tatou], [dupont]) | Afspraken inplannen en annuleren voor alle patiënten; alle namen in de agenda’s zien |
[USER] | Patiënten ([martin], [durand], [bistrou], [german]) | afspraken voor zichzelf boeken; hun afspraken annuleren; hun account beheren. In een agenda kunnen ze de namen van andere patiënten niet zien |
Demo-accounts gebruiken hun login als wachtwoord. De beheerregels worden door de server gecontroleerd, ongeacht de client:
- geen afspraken in het verleden;
- slechts één toekomstige afspraak per patiënt;
- een tijdvak kan per dag maar één keer worden geboekt;
- twee artsen (of twee cliënten) kunnen niet dezelfde voor- en achternaam hebben; een login hoort bij slechts één account;
- een arts met afspraken kan niet worden verwijderd;
- een record dat intussen door iemand anders is gewijzigd, kan niet worden overschreven (optimistische vergrendeling);
- aan een patiënt wordt nooit de naam van een andere patiënt toegewezen.
7.2. Installatie
U hebt [Node.js] en een [MySQL]-server (of [MariaDB]) nodig: de installatie ervan wordt beschreven in de bijlage.
1. De database. Het script [database/dbrdvmedecins2.sql] maakt de database [dbrdvmedecins2], de tabellen en de voorbeeldgegevens aan. Met [Laragon] voert u het uit vanuit [HeidiSQL] (menu File / Run a SQL file); op de opdrachtregel:
mysql -u root -p < database/dbrdvmedecins2.sql
Het script begint altijd met een lege database: u kunt het opnieuw uitvoeren om de oorspronkelijke gegevens te herstellen.
2. Bibliotheken. In een console die is geopend in de map [rdvmedecins-nestjs-json]:
3. Configuratie. Kopieer het bestand [.env.example] naar [.env] (onder [Windows]: [copy .env.example .env]) en wijzig het vervolgens:
| # -----------------------------------------------------------------------
# Voorbeeldconfiguratiebestand.
# Kopieer dit bestand naar [.env] en pas de waarden vervolgens aan uw machine aan.
# Het bestand [.env] mag nooit worden gedeeld of gecommit (wachtwoorden).
# -----------------------------------------------------------------------
# luisterpoort van de JSON-server (de Vue.js-client bereikt deze via de Vite-proxy)
PORT=8080
# verbindingsparameters voor de MySQL-database [dbrdvmedecins2]
# (script: database/dbrdvmedecins2.sql)
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE=dbrdvmedecins2
# true: toont in de console de SQL-query's die naar MySQL zijn gestuurd
DB_LOGGING=false
# -----------------------------------------------------------------------
# authenticatie
# -----------------------------------------------------------------------
# geheime sleutel voor het ondertekenen/verifiëren van JWT-tokens - MOET worden gewijzigd
# in productie (een lange willekeurige string, nooit gecommit).
JWT_SECRET=change-me-to-a-long-random-string
# geldigheid van een JWT-token, in seconden (7200 s = 2 uur). De cookie
# die het token draagt, heeft dezelfde levensduur.
JWT_EXPIRES_IN_SECONDS=7200
# in productie op true zetten (site geleverd via HTTPS): de authenticatiecookie
# wordt dan nooit over een onversleutelde verbinding verstuurd.
COOKIE_SECURE=false
# -----------------------------------------------------------------------
# bescherming tegen bots
# -----------------------------------------------------------------------
# login: aantal toegestane mislukkingen (verkeerde captcha of wachtwoord) voor
# dezelfde login vanaf hetzelfde IP-adres, voordat dit paar wordt geblokkeerd...
LOGIN_MAX_FAILURES=5
# ... gedurende deze periode (een IP-adres wordt ook geblokkeerd als het
# 5 keer meer mislukkingen verzamelt, alle logins samen)
LOGIN_BLOCK_MINUTES=15
# aanmaken van een clientaccount: maximumaantal accounts dat per uur wordt aangemaakt
# vanaf hetzelfde IP-adres. In een klaslokaal gaan alle computers vaak uit
# via hetzelfde adres: verhoog zo nodig.
REGISTRATION_MAX_PER_HOUR=10
# captcha-TESTMODUS: als deze variabele een waarde heeft, wordt die waarde
# geaccepteerd als antwoord op ELKE captcha. Handig om
# de API met curl te bevragen (in een console kunt u de afbeelding niet "lezen").
# In productie LEEG laten.
CAPTCHA_TEST=
# -----------------------------------------------------------------------
# productie
# -----------------------------------------------------------------------
# map van de gecompileerde Vue.js-client, door deze server geleverd (standaard: client/)
# CLIENT_DIR=client
# op true zetten als de server achter een proxy staat (nginx...)
TRUST_PROXY=false
|
- regel 8: de server luistert op poort 8080;
- Regels 12–16: de verbinding met [MySQL]. Met [Laragon] heeft de gebruiker [root] geen wachtwoord: de standaardwaarden volstaan;
- regel 25: de geheime sleutel waarmee de authenticatietokens worden ondertekend;
- regels 38–45: bescherming tegen bots (blokkering na meerdere mislukte inlogpogingen, aantal accounts dat per uur wordt aangemaakt);
- regel 51: de CAPTCHA-testmodus. De inlogpagina vereist dat gebruikers de tekens van een afbeelding overtypen, wat vanuit een console onmogelijk is. Als deze variabele een waarde heeft, wordt die waarde geaccepteerd als antwoord op elke CAPTCHA, vergelijkbaar met de „testsleutels” die [Google] beschikbaar stelt voor zijn [reCAPTCHA]. Voor de tests met [curl] in dit hoofdstuk voert u [CAPTCHA_TEST=test] in. Laat de variabele in productie LEEG;
- regels 57–59: twee instellingen voor productie (zie het hoofdstuk „Uitrol”).
4. Starten.
De console toont (fragment):
| [Nest] LOG [NestFactory] Starting Nest application...
[Nest] WARN [RdvMedecins] captcha TEST MODE active (CAPTCHA_TEST in .env): never use it in production
[Nest] LOG [RoutesResolver] ScheduleController {/api/schedule}:
[Nest] LOG [RouterExplorer] Mapped {/api/schedule, GET} route
[Nest] LOG [RoutesResolver] AppointmentsController {/api/appointments}:
...
[Nest] LOG [NestApplication] Nest application successfully started
[Nest] LOG [RdvMedecins] JSON server started: http://localhost:8080/api
|
Als de database niet bereikbaar is ([MySQL]-server gestopt, onjuist wachtwoord), stopt de server onmiddellijk met een foutmelding over de verbinding.
Alle URL’s beginnen met [/api]. Een geslaagd verzoek krijgt een statuscode 2xx en in de body de gevraagde gegevens in JSON-formaat. Een afgewezen verzoek krijgt een statuscode 4xx of 5xx en in de body gegevens in hetzelfde formaat:
| {
"statusCode": 400,
"key": "ERRORS.INVALID_FORM",
"params": { "min": "6" },
"fields": { "lastName": "ERRORS.LASTNAME_REQUIRED", "password": "ERRORS.PASSWORD_TOO_SHORT" }
}
|
- [statusCode]: de HTTP-code, herhaald in de body;
- [key]: de vertaalsleutel van het bericht. De server geeft nooit tekst terug die moet worden weergegeven: hij kent de taal van de gebruiker niet meer. De client vertaalt de sleutel met zijn woordenboeken (zie voorbeeld 16);
- [params] (optioneel): de parameters van het bericht ([At least {min} characters.], [Try again in {minutes} minute(s).]);
- [fields] (optioneel): bij een afgewezen formulier de fout voor elk veld.
Gebruikte codes:
Code | Betekenis | Voorbeelden van sleutels |
400 | Ongeldig verzoek | [ERRORS.INVALID_FORM] (+ fields), [ERRORS.INVALID_DATE], [ERRORS.WRONG_CAPTCHA], [ERRORS.CHOOSE_PATIENT], [ERRORS.WRONG_CURRENT_PASSWORD], [ERRORS.BAD_REQUEST]... |
401 | niet geauthenticeerd | [ERRORS.AUTH_REQUIRED] (geen token), [ERRORS.SESSION_EXPIRED] (ongeldig of verlopen token), [LOGIN.ERROR] |
403 | Toegang geweigerd | [ERRORS.FORBIDDEN] (onvoldoende rol), [ERRORS.CAPTCHA_BEFORE_ACCOUNT] (geen toegangspas) |
404 | Niet gevonden | [ERRORS.NOT_FOUND], [ERRORS.DOCTOR_NOT_FOUND], [ERRORS.APPOINTMENT_NOT_FOUND]... |
409 | Schending van een bedrijfsregel | [ERRORS.PAST_DATE], [ERRORS.SLOT_ALREADY_BOOKED], [ERRORS.LOGIN_TAKEN], [ERRORS.CONCURRENT_UPDATE]... |
415 | POST die niet in JSON is | [ERRORS.JSON_REQUIRED] |
429 | Te veel pogingen | [ERRORS.TOO_MANY_ATTEMPTS], [ERRORS.TOO_MANY_ACCOUNTS] (+ params.minutes) |
500 | serverfout | [ERRORS.SERVER_ERROR] |
7.4. Authenticatie
De client authenticeert zich met [POST /api/auth/login], met een gebruikersnaam, een wachtwoord en een CAPTCHA-antwoord. In ruil daarvoor plaatst de server een cookie in de browser die een ondertekend JWT-token bevat:
| Set-Cookie: rdvmedecins_jwt=eyJhbGciOiJIUzI1NiIs...; Max-Age=7200; Path=/api; HttpOnly; SameSite=Strict
|
De attributen ervan zijn belangrijk voor de beveiliging:
- [HttpOnly]: de [JavaScript]-code van de pagina kan deze cookie niet lezen. Een kwaadaardig script dat in de pagina is geïnjecteerd, zou het token dus niet kunnen stelen. Gevolg voor de client: die kan niet zelf vaststellen of hij is ingelogd. Hij vraagt het aan de server ([GET /api/auth/me]);
- [Path=/api]: de browser voegt de cookie alleen toe aan verzoeken naar de API;
- [SameSite=Strict]: de browser voegt dit token alleen toe aan verzoeken die worden verstuurd VANAF een pagina van deze site. Een kwaadaardige pagina op een andere site kan niet laten gebeuren dat de browser namens de gebruiker handelt (een zogeheten CSRF-aanval);
- [Max-Age=7200]: de cookie verloopt na twee uur, op hetzelfde moment als het token.
De browser stuurt deze cookie vervolgens automatisch mee met elk verzoek naar [/api]. De [Vue.js]-client hoeft dus niets te doen om zich te authenticeren, mits de pagina’s en de API dezelfde origin delen (hetzelfde protocol, dezelfde machine, dezelfde poort). Dit is de rol van de [Vite]-proxy tijdens de ontwikkeling (zie voorbeeld 17) en van de server zelf in productie (zie het hoofdstuk „Uitrol”).
Een tweede bescherming tegen CSRF-aanvallen: de server weigert (415) elk POST-verzoek waarvan de body niet in JSON is. Een gecompromitteerd HTML-formulier op een andere site kan geen JSON verzenden.
7.5. De API
Methode | Adres | Rollen | Verzonden body / antwoord |
GET | [/api/captcha] | openbaar | Antwoord: [{ id, svg }] |
POST | [/api/auth/login] | openbaar | [{ login, password, captchaId, captcha }]; antwoord: [{ login, name, role }] + cookie |
POST | [/api/auth/logout] | openbaar | [{}] ; verwijdert de cookie |
GET | [/api/auth/me] | ingelogd | antwoord: [{ login, name, role }] |
GET | [/api/schedule?doctorId=&day=] | alle | Antwoord: de agenda (zonder doctorId: voor een DOCTOR de eigen agenda) |
POST | [/api/appointments] | alle | [{ day, slotId, clientId }] (clientId genegeerd voor een USER); 201 |
DELETE | [/api/appointments/:id] | alle | Een USER annuleert alleen eigen afspraken |
GET | [/api/doctors] | alle | de lijst (met login voor ADMIN) |
GET | [/api/clients] | ADMIN, DOCTOR | de lijst (met login voor ADMIN) |
GET | [/api/doctors/:id], [/api/clients/:id] | ADMIN | een gegevensblad |
POST | [/api/doctors], [/api/clients] | ADMIN | [{ title, firstName, lastName, login?, password? }]; 201 |
PUT | [/api/doctors/:id], [/api/clients/:id] | ADMIN | [{ title, firstName, lastName, login?, password?, version }] |
DELETE | [/api/doctors/:id], [/api/clients/:id] | ADMIN | Antwoord: [{}] |
POST | [/api/register/access] | openbaar | [{ captchaId, captcha }]; geeft een token uit (cookie) |
GET | [/api/register/access] | openbaar | antwoord: [{ valid }] |
POST | [/api/register] | openbaar + toegangspas | [{ title, firstName, lastName, login, password, confirmation }]; 201 |
GET | [/api/my-area] | USER | antwoord: [{ patient, appointments }] |
GET | [/api/my-account] | USER | Antwoord: [{ title, firstName, lastName, login, version }] |
PUT | [/api/my-account] | USER | [{ title, firstName, lastName, login, password?, confirmation?, currentPassword, version }] |
DELETE | [/api/my-account] | USER | [{ currentPassword }]; verwijdert het account en de afspraken ervan |
Een agenda-item ([GET /api/schedule]) beschrijft een tijdvak EN wat de ingelogde gebruiker ermee mag doen:
| { "id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0,
"free": false,
"patient": { "title": "Mme", "firstName": "Alice", "lastName": "DURAND" },
"mine": false, "bookable": false, "appointmentId": 2 }
|
- [free]: is het tijdvak op die dag beschikbaar?
- [patient]: de patiënt voor de afspraak. Voor een ingelogde patiënt is dit gelijk aan [null], behalve voor ZIJN EIGEN afspraak: de namen van andere patiënten worden nooit verzonden;
- [mine]: dit is de afspraak van de ingelogde patiënt;
- [bookable]: kan de gebruiker dit tijdvak boeken (beschikbaar, niet in het verleden en, voor een patiënt, geen toekomstige afspraak)?
- [appointmentId]: ingesteld als de gebruiker deze afspraak kan annuleren.
De server BESLIST; de client geeft het weer. En zelfs als een client zou valsspelen (door een knop „Boeken” weer te geven terwijl dat niet mag), controleert de server bij het boeken alles opnieuw.
7.6. De API bevragen met curl
De map [curl] op de server bevat de JSON-bodies van de verzoeken uit dit hoofdstuk, één bestand per verzoek. Bijvoorbeeld [curl/login-admin.json]:
| { "login": "admin", "password": "admin", "captchaId": "-", "captcha": "test" }
|
De opdrachten worden ingevoerd in een console die is geopend in de map [rdvmedecins-nestjs-json]. Enkele opties van [curl]:
- [-i]: toon ook de headers van het antwoord; [-s]: zonder voortgangsbalk;
- [--json @file]: verstuur de inhoud van het bestand met POST, met de header [Content-Type: application/json] (curl 7.82 en later; bij een oudere versie gebruikt u [-H "Content-Type: application/json" -d @file]);
- [-X PUT], [-X DELETE]: de HTTP-methode;
- [-c cookies.txt]: sla de ontvangen cookies op in dit bestand; [-b cookies.txt]: stuur ze terug. Zo blijft [curl] „ingelogd” van de ene opdracht naar de volgende, net zoals een browser dat zou doen.
Voor [PowerShell] moet u [curl.exe] invoeren, niet [curl] (dat naar een ander commando verwijst). De nogal uitvoerige JSON-antwoorden zijn voor dit document opgemaakt en de headers [Date], [ETag], [Connection], en [Keep-Alive] zijn verwijderd.
7.6.1. Inloggen
Eerst de CAPTCHA (de afbeelding is bijgesneden):
| $ curl -i http://localhost:8080/api/captcha
HTTP/1.1 200 OK
Cache-Control: no-store
Content-Type: application/json; charset=utf-8
Content-Length: 15076
{"id": "abf00f0c-60c0-4fde-9782-9906815e26a1", "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"150..."}
|
Zonder token geeft een beveiligde URL een 401-antwoord:
| $ curl -i http://localhost:8080/api/doctors
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Content-Length: 47
{"statusCode": 401, "key": "ERRORS.AUTH_REQUIRED"}
|
Een onjuiste CAPTCHA, gevolgd door een onjuist wachtwoord:
| $ curl -i --json @curl/login-wrong-captcha.json http://localhost:8080/api/auth/login
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 91
{"statusCode": 400, "key": "ERRORS.WRONG_CAPTCHA", "fields": {"captcha": "ERRORS.WRONG_CAPTCHA"}}
|
| $ curl -i --json @curl/login-wrong-password.json http://localhost:8080/api/auth/login
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Content-Length: 38
{"statusCode": 401, "key": "LOGIN.ERROR"}
|
Een geslaagde aanmelding plaatst de cookie, die [curl] opslaat in [cookies.txt]:
| $ curl -i -c cookies.txt --json @curl/login-admin.json http://localhost:8080/api/auth/login
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=eyJhbGciOiJIUzI1NiIs...; Max-Age=7200; Path=/api; Expires=Mon, 28 Sep 2026 12:49:12 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 55
{"login": "admin", "name": "Administrator", "role": "ADMIN"}
|
Met deze cookie weet de server wie we zijn:
| $ curl -i -b cookies.txt http://localhost:8080/api/auth/me
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 55
{"login": "admin", "name": "Administrator", "role": "ADMIN"}
|
Een vervalst (of verlopen) token leidt tot een verlopen sessie, en de server geeft de browser de opdracht de cookie te verwijderen:
| $ curl -i -b "rdvmedecins_jwt=abc.def.ghi" http://localhost:8080/api/auth/me
HTTP/1.1 401 Unauthorized
Set-Cookie: rdvmedecins_jwt=; Path=/api; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 49
{"statusCode": 401, "key": "ERRORS.SESSION_EXPIRED"}
|
7.6.2. Artsen, cliënten, agenda
De lijst met artsen, gezien door ADMIN (met logins):
| $ curl -i -b cookies.txt http://localhost:8080/api/doctors
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 282
[
{"id": 3, "version": 0, "title": "Melle", "firstName": "Sophie", "lastName": "DUPONT", "login": "dupont"},
{"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER", "login": "pelissier"},
{"id": 2, "version": 0, "title": "Mr", "firstName": "Paul", "lastName": "TATOU", "login": "tatou"}
]
|
De agenda van dr. PELISSIER voor 15 januari 2030:
| $ curl -s -b cookies.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-01-15"
{
"doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
"day": "2030-01-15",
"slots": [
{"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
],
"info": null
}
|
Een datum die niet bestaat:
| $ curl -i -b cookies.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-02-30"
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 46
{"statusCode": 400, "key": "ERRORS.INVALID_DATE"}
|
7.6.3. Afspraken
ADMIN reserveert tijdvak 3 voor patiënt nr. 2 (Alice DURAND). De body van het verzoek:
| { "day": "2030-01-15", "slotId": 3, "clientId": 2 }
|
| $ curl -i -b cookies.txt --json @curl/appointment-admin.json http://localhost:8080/api/appointments
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 51
{"id": 2, "day": "2030-01-15", "slotId": 3, "clientId": 2}
|
De agenda toont nu deze afspraak, samen met de naam van de patiënt en instructies om de afspraak te annuleren ([appointmentId]):
| $ curl -s -b cookies.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-01-15"
{
"doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
"day": "2030-01-15",
"slots": [
{"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": false, "patient": {"title": "Mme", "firstName": "Alice", "lastName": "DURAND"}, "mine": false, "bookable": false, "appointmentId": 2},
{"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
],
"info": null
}
|
Probeer het opnieuw: de patiënt heeft al een toekomstige afspraak. Een afspraak in het verleden wordt eveneens geweigerd:
| $ curl -i -b cookies.txt --json @curl/appointment-admin.json http://localhost:8080/api/appointments
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 66
{"statusCode": 409, "key": "ERRORS.PATIENT_HAS_UPCOMING_APPOINTMENT"}
|
| $ curl -i -b cookies.txt --json @curl/appointment-past.json http://localhost:8080/api/appointments
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 43
{"statusCode": 409, "key": "ERRORS.PAST_DATE"}
|
Een standaard HTML-formulier (hier gesimuleerd met [-d], dat [application/x-www-form-urlencoded] verstuurt) wordt geweigerd:
| $ curl -i -b cookies.txt -d "day=2030-01-15&slotId=4&clientId=2" http://localhost:8080/api/appointments
HTTP/1.1 415 Unsupported Media Type
Content-Type: application/json; charset=utf-8
Content-Length: 47
{"statusCode": 415, "key": "ERRORS.JSON_REQUIRED"}
|
7.6.4. Het perspectief van een patiënt
U logt uit en logt vervolgens in als [martin]:
| $ curl -i -b cookies.txt -c cookies.txt --json @curl/empty.json http://localhost:8080/api/auth/logout
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=; Path=/api; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2
{}
|
| $ curl -s -c cookies-martin.txt --json @curl/login-martin.json http://localhost:8080/api/auth/login
{"login": "martin", "name": "Jean MARTIN", "role": "USER"}
|
Dezelfde agenda, gezien door een patiënt: tijdvak 3 is bezet (["free": false]), maar ["patient"] is gelijk aan [null]: de naam van Alice, DURAND, is niet meegestuurd:
| $ curl -s -b cookies-martin.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-01-15"
{
"doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
"day": "2030-01-15",
"slots": [
{"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": false, "patient": null, "mine": false, "bookable": false, "appointmentId": null},
{"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
],
"info": null
}
|
Een patiënt heeft geen toegang tot de cliëntenlijst:
| $ curl -i -b cookies-martin.txt http://localhost:8080/api/clients
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 43
{"statusCode": 403, "key": "ERRORS.FORBIDDEN"}
|
De patiënt reserveert voor zichzelf (de body bevat geen [clientId]), eenmaal, niet tweemaal:
| { "day": "2030-01-15", "slotId": 5 }
|
| $ curl -i -b cookies-martin.txt --json @curl/appointment-martin.json http://localhost:8080/api/appointments
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 51
{"id": 3, "day": "2030-01-15", "slotId": 5, "clientId": 1}
|
| $ curl -i -b cookies-martin.txt --json @curl/appointment-martin-2.json http://localhost:8080/api/appointments
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 55
{"statusCode": 409, "key": "SCHEDULE.ALREADY_BOOKED_INFO"}
|
Zijn omgeving:
| $ curl -s -b cookies-martin.txt http://localhost:8080/api/my-area
{
"patient": {"title": "Mr", "firstName": "Jean", "lastName": "MARTIN"},
"appointments": [
{
"id": 3,
"day": "2030-01-15",
"slot": {"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40},
"doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"}
}
]
}
|
Hij kan de afspraak van een andere patiënt niet annuleren. De server antwoordt met „niet gevonden”, net zoals bij een afspraak die niet bestaat: er wordt niets onthuld over de afspraken van andere patiënten:
| $ curl -i -b cookies-martin.txt -X DELETE http://localhost:8080/api/appointments/2
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Content-Length: 55
{"statusCode": 404, "key": "ERRORS.APPOINTMENT_NOT_FOUND"}
|
Zijn eigen afspraak wel:
| $ curl -i -b cookies-martin.txt -X DELETE http://localhost:8080/api/appointments/3
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2
{}
|
7.6.5. Het perspectief van een arts
Zonder [doctorId] ontvangt een ingelogde arts ZIJN EIGEN agenda, met alle namen:
| $ curl -s -b cookies-pelissier.txt "http://localhost:8080/api/schedule?day=2030-01-15"
{
"doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
"day": "2030-01-15",
"slots": [
{"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": false, "patient": {"title": "Mme", "firstName": "Alice", "lastName": "DURAND"}, "mine": false, "bookable": false, "appointmentId": 2},
{"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": false, "patient": {"title": "Mr", "firstName": "Jean", "lastName": "MARTIN"}, "mine": false, "bookable": false, "appointmentId": 3},
{"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
{"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
],
"info": null
}
|
Het beheren van artsen is niet toegestaan:
| $ curl -i -b cookies-pelissier.txt --json @curl/doctor-add.json http://localhost:8080/api/doctors
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 43
{"statusCode": 403, "key": "ERRORS.FORBIDDEN"}
|
7.6.6. Artsen en cliënten beheren (ADMIN)
Een toevoeging. Als de gebruikersnaam ontbreekt, is de standaardwaarde de naam in kleine letters; het wachtwoord is de gebruikersnaam:
| { "title": "Mme", "firstName": "Claire", "lastName": "LEROY" }
|
| $ curl -i -b cookies.txt --json @curl/doctor-add.json http://localhost:8080/api/doctors
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 90
{"id": 4, "version": 1, "title": "Mme", "firstName": "Claire", "lastName": "LEROY", "login": "leroy"}
|
Ongeldige body: het antwoord vermeldt de fout per veld:
| { "title": "Dr", "firstName": "", "lastName": "A-LAST-NAME-FAR-TOO-LONG-FOR-THE-DB", "login": "Claire L" }
|
| $ curl -i -b cookies.txt --json @curl/doctor-invalid.json http://localhost:8080/api/doctors
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 218
{
"statusCode": 400,
"key": "ERRORS.INVALID_FORM",
"params": {"min": "6"},
"fields": {"login": "ERRORS.INVALID_LOGIN", "title": "ERRORS.INVALID_TITLE", "firstName": "ERRORS.FIRSTNAME_REQUIRED", "lastName": "ERRORS.LASTNAME_TOO_LONG"}
}
|
Een homoniem: beide betrokken velden worden gemarkeerd:
| $ curl -i -b cookies.txt --json @curl/doctor-duplicate.json http://localhost:8080/api/doctors
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 151
{"statusCode": 409, "key": "ERRORS.DOCTOR_ALREADY_EXISTS", "fields": {"firstName": "ERRORS.DOCTOR_ALREADY_EXISTS", "lastName": "ERRORS.DOCTOR_ALREADY_EXISTS"}}
|
Een wijziging. De [version] is de waarde die uit het gegevensblad is gelezen (hier 1):
| { "title": "Mme", "firstName": "Claire", "lastName": "LEROY-MARTIN", "version": 1 }
|
| $ curl -i -b cookies.txt -X PUT --json @curl/doctor-edit.json http://localhost:8080/api/doctors/4
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 97
{"id": 4, "version": 2, "title": "Mme", "firstName": "Claire", "lastName": "LEROY-MARTIN", "login": "leroy"}
|
Het record heeft nu versie 2. Dezelfde wijziging opnieuw indienen (met versie 1) zou een record overschrijven dat intussen door iemand anders is gewijzigd: de optimistische vergrendeling voorkomt dit:
| $ curl -i -b cookies.txt -X PUT --json @curl/doctor-edit.json http://localhost:8080/api/doctors/4
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 51
{"statusCode": 409, "key": "ERRORS.CONCURRENT_UPDATE"}
|
Een arts met afspraken kan niet worden verwijderd; de overige artsen wel:
| $ curl -i -b cookies.txt -X DELETE http://localhost:8080/api/doctors/1
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 57
{"statusCode": 409, "key": "ERRORS.DOCTOR_HAS_APPOINTMENTS"}
|
| $ curl -i -b cookies.txt -X DELETE http://localhost:8080/api/doctors/4
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2
{}
|
7.6.7. Een account aanmaken
Zonder toegangsbewijs is dat onmogelijk:
| $ curl -i --json @curl/registration.json http://localhost:8080/api/register
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 56
{"statusCode": 403, "key": "ERRORS.CAPTCHA_BEFORE_ACCOUNT"}
|
Eerst lost u een CAPTCHA op; de server geeft een token uit, 15 minuten geldig, in een cookie:
| $ curl -i -c cookies-registration.txt --json @curl/captcha-test.json http://localhost:8080/api/register/access
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_registration=06be5557-401a-4bd4-ada2-277ca48fc74d; Max-Age=900; Path=/api/register; Expires=Mon, 28 Sep 2026 11:04:12 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 2
{}
|
| $ curl -s -b cookies-registration.txt http://localhost:8080/api/register/access
{"valid": true}
|
De bevestiging van het wachtwoord moet identiek zijn:
| $ curl -i -b cookies-registration.txt --json @curl/registration-confirmation.json http://localhost:8080/api/register
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 98
{"statusCode": 400, "key": "ERRORS.INVALID_FORM", "fields": {"confirmation": "ERRORS.PASSWORDS_DIFFER"}}
|
| { "title": "Mme", "firstName": "Julie", "lastName": "BERNARD", "login": "jbernard", "password": "secret1", "confirmation": "secret1" }
|
| $ curl -i -b cookies-registration.txt -c cookies-registration.txt --json @curl/registration.json http://localhost:8080/api/register
HTTP/1.1 201 Created
Set-Cookie: rdvmedecins_registration=; Path=/api/register; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 20
{"login": "jbernard"}
|
Het toegangsbewijs is gebruikt: daarmee kan geen tweede account worden aangemaakt:
| $ curl -i -b cookies-registration.txt --json @curl/registration.json http://localhost:8080/api/register
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 56
{"statusCode": 403, "key": "ERRORS.CAPTCHA_BEFORE_ACCOUNT"}
|
7.6.8. Mijn account beheren
De nieuwe patiënt logt in en leest haar gegevens:
| $ curl -s -b cookies-jbernard.txt http://localhost:8080/api/my-account
{"title": "Mme", "firstName": "Julie", "lastName": "BERNARD", "login": "jbernard", "version": 1}
|
Voor een wijziging is het huidige wachtwoord vereist. De server geeft een nieuw token uit, dat de nieuwe naam bevat:
| { "title": "Mme", "firstName": "Julie", "lastName": "BERNARD-DUVAL", "login": "jbernard", "currentPassword": "secret1", "version": 1 }
|
| $ curl -i -b cookies-jbernard.txt -c cookies-jbernard.txt -X PUT --json @curl/my-account.json http://localhost:8080/api/my-account
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=eyJhbGciOiJIUzI1NiIs...; Max-Age=7200; Path=/api; Expires=Mon, 28 Sep 2026 12:49:13 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 63
{"login": "jbernard", "name": "Julie BERNARD-DUVAL", "role": "USER"}
|
Het verwijderen van het account verwijdert ook de cookie:
| $ curl -i -b cookies-jbernard.txt -c cookies-jbernard.txt -X DELETE --json @curl/my-account-delete.json http://localhost:8080/api/my-account
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=; Path=/api; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2
{}
|
| $ curl -i -b cookies-jbernard.txt http://localhost:8080/api/auth/me
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Content-Length: 47
{"statusCode": 401, "key": "ERRORS.AUTH_REQUIRED"}
|
7.6.9. Enkele fouten
Een onbekende route, een body die geen geldige JSON is:
| $ curl -i -b cookies-martin.txt http://localhost:8080/api/does-not-exist
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Content-Length: 43
{"statusCode": 404, "key": "ERRORS.NOT_FOUND"}
|
| $ curl -i -b cookies.txt -H "Content-Type: application/json" -d "{not json" http://localhost:8080/api/appointments
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 45
{"statusCode": 400, "key": "ERRORS.BAD_REQUEST"}
|
We weten nu alles wat we over de server moeten weten om onze client te schrijven.