Dynamic Web Development with Seaside

24.3.3Request Path

URIs are a powerful mechanism to specify hierarchical information. They allow one to specify and access to specific resources. Seaside-Rest offers a number of methods to support the manipulation of URIs. Some predefined methods are invoked by Seaside when you define them in your service.

The method list we implemented in Section 24.2.2 is executed when the URI does not contain any access path beside the one of the application.

ToDoHandler>>list
<get>

^ String streamContents: [ :stream |
ToDoList default items do: [ :each |
stream nextPutAll: each title; crlf ] ]

If we define services with methods that expect multiple arguments, the arguments get mapped to the unconsumed path elements. In the example below we use the first path element to identify a todo item by title, and then perform an action on it using the second path element:

ToDoHandler>>command: aTitleString action: anActionString
<get>

| item |
item := ToDoList default items
detect: [ :each | each title = aTitleString ]
ifNone: [ ^ 'unknown todo item' ].
anActionString = 'isDone' ifTrue: [
^ item done
ifTrue: [ 'done' ]
ifFalse: [ 'todo' ] ].
...
^ 'invalid command'

Now we can query the model like in the following examples:

$ curl http://localhost:8080/todo-api/Invalid/isDone
unknown todo item
$ curl http://localhost:8080/todo-api/Discuss+cover+design/isDone
done
$ curl http://localhost:8080/todo-api/Annotate+first+chapter/isDone
todo

Copyright © 19 March 2024 Stéphane Ducasse, Lukas Renggli, C. David Shaffer, Rick Zaccone
This book is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 license.

This book is published using Seaside, Magritte and the Pier book publishing engine.