WebServer and WebClient hello world demo
Last updated at 1:02 pm UTC on 16 March 2019
Video demonstrating how to use WebServer and WebClient objects. At the beginning it is mentioned that you need to load the WebServer and WebClient code. This is no longer necessary as it is included in current versions of Squeak.
The following example is taken from the help system.
Starting the web server
A WebServer is started by listening on a particular port. The examples below use 'WebServer reset default' for convenience; specific applications should have their own registry for accessing various WebServer instances.
(WebServer reset default)
The server will persist when the image is restarted and must be stopped explicitly by sending it the #destroy message.
Once the server is running, you can point your browser to http://localhost:8080 but since we haven't told WebServer what we'd like to do all we get is a 404 (not found) response. To tell WebServer what to do we need to register a service:
WebServer default addService: '/hello' action:[:req|
req send200Response: 'Hello World'.
The service takes a path (/hello) and an action to perform for any request that maps to this path. We can now fetch the response in a browser by going to http://localhost:8080/hello or directly using WebClient:
(WebClient httpGet:'http://localhost:8080/hello') class
(WebClient httpGet:'http://localhost:8080/hello') content inspect.
(WebClient httpGet:'http://localhost:8080/hello/squeak') content.
In Cuis port: