Dynamic Web Development with Seaside

24.5Request and Response

Accessing and exploring HTTP requests emitted by a client is an important task during the development of a REST web service. The request gives access to information about the client (IP address, HTTP agent, ...).

To access the request we can add the expression self requestContext request inspect anywhere into Seaside code. This works inside a WAComponent as well as inside a WARestfulHandler.

When the method is executed, you get an inspector on the current request as shown in Figure 158.

Inspecting a request

The following example uses the expression headers at: 'content-type' that returns the Content-Type present in the inspected HTTP client request.


self requestContext request inspect.
^ String streamContents: [ :stream |

In the case of the transmission of a form (corresponding to the application/x-www-form-urlencoded MIME type), we can access the submitted fields using the message postFields.

It is also possible to customize the HTTP response. A common task is to set the HTTP response code to indicate a result to the client.

For example we could implement a delete operation for our todo items as follows:

ToDoHandler>>delete: aString

| item |
item := ToDoList default items
detect: [ :each | each title = aString ]
ifNone: [ nil ].
self requestContext respond: [ :response |
item isNil
ifTrue: [ response status: WARequest statusNotFound ]
ifFalse: [
ToDoList default remove: item.
response status: WARequest statusOk ] ]

The different status codes are implemented on the class side of WARequest. They are grouped in five main families with numbers between 100 and 500. The most common one is status code WARequest statusOk (200) and WARequest statusNotFound (404).

Copyright © 18 April 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.