5. Links, forms and unicorns
Building Hypermedia APIs
Why links and forms will make your
ReSTful API better
6.
7.
8.
9. Your mission should you accept it…
• Go to Paris, give the agent your name and ask
for his favourite monument
• Go to Brussels, tell the agent what you learnt,
ask for their favourite food
• Go to Copenhagen, tell them what you learnt,
ask for their favourite painter
• In Malmo, tell them what you learnt, receive
the secret message
10. Your mission should you accept it…
• POST to /paris, expect {“status”: “ok”,
“monument”: ??}
• POST to /brussels with {“monument”: ??},
expect {“status”: “ok”, “food”: ??}
• POST to /copenhagen with {“food”: ??},
expect {“status”:”ok”, “artist”: ??}
• POST to /malmo with {“artist”: ??}, expect
{“status”: “ok”, “secret”: ??}
11. public void travel() {
var response = _client
.Post(“/paris”, new{name=“Pixie”})
.Send();
if (response.body.status == “ok”) {
response = _client.Post(“/brussels”, new {
monument=response.body.monument
}).Send();
// etc
}
}
21. We could send an agent to
Brussels to warn Resty Galore…
22. > POST /brussels
> {“monument”: “eiffel tower”}
< 307 Redirect to London, quick!
23. Keeping agents in each location we need to
redirect is expensive.
Travelling for nothing is also expensive.
Or Resty Galore could start her journey with…
24. Keeping agents in each location we need to
redirect is expensive.
Or Resty Galore could start her journey with…
25.
26. > POST /paris
> {“name”: “resty galore”}
< 200 OK
< Link: </london>; rel=“next”
< {“monument”: “eiffel tower”}
27. public void travel() {
var response = _client
.Post(“/paris”, new{name=“Pixie”})
.Send();
if (response.status == 200) {
response = _client.Post(response.links.next, new {
monument = response.body.monument
}).Send();
// etc
}
}
28. > POST /london
> {“monument”: “eiffel tower”}
< 418 I’m a teapot
<
<
<
<
<
29. > POST /london
> {“monument”: “eiffel tower”}
< 200 OK
< Link: </copenhagen>; rel=“next”
< {“food”: “Chicken Tikka Massala”}
42. > GET /malmo
< 200 OK
< {“form”: {
< “method”: “DELETE”
< “href”: “/malmo/mole”
< }}
> DELETE /malmo/mole
> 200 OK
< {“secret”: “ReST is amazing.”}
43.
44.
45. We lowered coupling by…
• Respecting the Uniform Interface (status
codes, verbs…)
• Introducing Links to navigate
• Introducing forms to know what to send
• Introducing control data in forms to enable a
change in workflow with no change in the
client code.