### Basic Teapot Server Setup Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Initializes a Teapot web server with a simple 'Hello World!' GET route and starts the server. No external dependencies are required for this basic setup. ```smalltalk Teapot on GET: '/welcome' -> 'Hello World!'; start. ``` -------------------------------- ### Teapot CRUD Example (RESTful API) Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This comprehensive example demonstrates building a RESTful API with Teapot for managing books. It covers GET, PUT, and DELETE operations, along with exception handling for 'KeyNotFound'. The example also configures Teapot with JSON output and a specific port. ```smalltalk books := Dictionary new. teapot := Teapot configure: { #defaultOutput -> #json. #port -> 8080. #debugMode -> true. #bindAddress -> #[127 0 0 1]. }. teapot GET: '/books' -> books; PUT: '/books/' -> [:req | book := {'author' -> (req at: #author). 'title' -> (req at: #title)} asDictionary. books at: (req at: #id) put: book]; DELETE: '/books/' -> [:req | books removeKey: (req at: #id)]; exception: KeyNotFound -> (TeaResponse notFound body: 'No such book'); start. ``` -------------------------------- ### Basic Route Definition Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Demonstrates how to define a simple route for a GET request that responds with 'Hello World!'. ```APIDOC ## POST /api/users ### Description Defines a basic route for a GET request to the '/welcome' endpoint. ### Method GET ### Endpoint /welcome ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **message** (string) - A greeting message. #### Response Example ```json { "message": "Hello World!" } ``` ``` -------------------------------- ### Teapot Response Transformers Examples Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Configures a Teapot server with different response transformers for various routes. It demonstrates JSON output for a list, plain text for a string, and streaming for a file. It also shows how to fetch the content from these routes. ```smalltalk Teapot on GET: '/jsonlist' -> #(1 2 3 4); output: #json; GET: '/sometext' -> 'this is text plain'; output: #text; GET: '/download' -> ['/tmp/afile' asFileReference readStream]; output: #stream; start. (ZnEasy get: 'http://localhost:1701/jsonlist') entity string. "prints json array: '[1,2,3,4]'" ZnEasy get: 'http://localhost:1701/download' "a ZnResponse(200 OK application/octet-stream 35B)" ``` -------------------------------- ### Teapot Template Rendering Example Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Sets up a Teapot route that uses a Mustache template to render dynamic HTML content. The route provides a dictionary of variables and specifies a `mustacheHtml` output transformer. ```smalltalk Teapot on GET: '/greet' -> {'phrase' -> 'Hello'. 'name' -> 'World'}; output: (TeaOutput mustacheHtml: '{{phrase}} {{name}}!'); start. ``` -------------------------------- ### Handle POST and Other Methods in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This snippet illustrates handling POST requests and other HTTP methods in Teapot. It shows a login form implemented with GET and the form submission handled by POST. The example highlights how the `at:` method on the request object can uniformly access parameters. ```smalltalk Teapot on GET: '/login' -> '
User name:

Password:

'; POST: '/login'-> [ :req | 'Welcome ', (req at: #user) ]; start. ``` -------------------------------- ### Teapot Route Matching and Response Handling Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Illustrates different ways to define a route that returns 'Hello World!'. These examples highlight how Teapot can handle responses as plain objects, TeaResponses, or ZnResponses, and how the default HTML transformer works. ```smalltalk GET: '/greet' -> [:req | 'Hello World!' ] GET: '/greet' -> [:req | TeaResponse ok body: 'Hello World!' ] GET: '/greet' -> [:req | ZnResponse new statusLine: ZnStatusLine ok; entity: (ZnEntity html: 'Hello World!'); yourself ] ``` -------------------------------- ### Teapot Route Definition Examples Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Demonstrates various ways to define routes in Teapot, including simple string responses, dynamic responses using blocks with request parameters, and sending messages to objects. It also shows how to make an HTTP request to a defined route. ```smalltalk Teapot on GET: '/hi' -> 'Bonjour!'; GET: '/hi/' -> [:req | 'Hello ', (req at: #user)]; GET: '/say/hi/*' -> (Send message: #greet: to: greeter); start. (ZnEasy get: 'http://localhost:1701/hi/user1') entity string. "Hello user1" ``` -------------------------------- ### Teapot Request Abort Examples Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Demonstrates how to use the `abort:` message within Teapot routes to immediately stop request processing and return a specific response. It shows aborting with a predefined `TeaResponse` and with a simple string. ```smalltalk Teapot on GET: '/secure/*' -> [:req | req abort: TeaResponse unauthorized]; GET: '/unauthorized' -> [:req | req abort: 'go away' ]; start. ``` -------------------------------- ### Configure After Filters in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This code illustrates the setup of 'after' filters in Teapot. These filters run after a request has been processed and can modify the response, such as adding custom headers. The example adds an 'X-Foo' header to the response. ```smalltalk Teapot on after: '/*' -> [:req :resp | resp headers at: 'X-Foo' put: 'set by after filter']; start. ``` -------------------------------- ### Client Request to Teapot API (PUT) Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This snippet shows how to use ZnClient to interact with the Teapot REST API created in the previous example. It demonstrates making a PUT request to create or update a book, sending form data for the author and title. ```smalltalk ZnClient new url: 'http://localhost:8080/books/1'; formAt: 'author' put: 'SquareBracketAssociates'; formAt: 'title' put: 'Pharo For The Enterprise'; put ``` -------------------------------- ### Use Query Parameters in Teapot Routes Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This snippet shows how to define routes in Teapot that accept query parameters. The example route '/books' uses '#title' and '#limit' parameters from the request. It also mentions using `at:ifAbsent:` to handle cases where parameters might be missing. ```smalltalk Teapot on GET: '/books' -> [:req | books findByTitle: (req at: #title) limit: (req at: #limit) ]; start. "matches: http://localhost:1701/books?title=smalltalk&limit=12" ``` -------------------------------- ### Route with Path Parameter Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Shows how to define a route that accepts a path parameter and uses it in the response. ```APIDOC ## GET /hi/ ### Description Defines a route that captures a 'user' from the URL path and includes it in the greeting. ### Method GET ### Endpoint /hi/ ### Parameters #### Path Parameters - **user** (string) - Required - The name of the user to greet. #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **message** (string) - A personalized greeting. #### Response Example ```json { "message": "Hello user1" } ``` ``` -------------------------------- ### Handling POST and Other Methods Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Demonstrates how to handle different HTTP methods like POST, PUT, and DELETE, including accessing form data. ```APIDOC ## Handling POST and Other Methods ### Description Demonstrates handling various HTTP methods (POST, PUT, DELETE) and accessing request parameters, including form data. ### Method GET, POST, PUT, DELETE ### Endpoint - `/login` (GET, POST) - `/books` (GET) - `/books/` (PUT, DELETE) ### Parameters #### Path Parameters - `id` (String) - Required - The unique identifier for a book. #### Query Parameters None explicitly shown in examples, but `req at:` can access them. #### Request Body - `user` (String) - Required (for POST /login) - The username from the form. - `pwd` (String) - Required (for POST /login) - The password from the form. - `author` (String) - Required (for PUT /books/) - The author of the book. - `title` (String) - Required (for PUT /books/) - The title of the book. ### Request Example ```smalltalk books := Dictionary new. teapot := Teapot configure: { #defaultOutput -> #json. #port -> 8080. #debugMode -> true. #bindAddress -> #[127 0 0 1]. }. teapot GET: '/login' -> '
User name:

Password:

'; POST: '/login'-> [ :req | 'Welcome ', (req at: #user) ]; GET: '/books' -> books; PUT: '/books/' -> [:req | | book | book := {'author' -> (req at: #author). 'title' -> (req at: #title)} asDictionary. books at: (req at: #id) put: book]; DELETE: '/books/' -> [:req | books removeKey: (req at: #id)]; exception: KeyNotFound -> (TeaResponse notFound body: 'No such book'); start. "Client example for creating a book:" (ZnClient new url: 'http://localhost:8080/books/1'; formAt: 'author' put: 'SquareBracketAssociates'; formAt: 'title' put: 'Pharo For The Enterprise'; put) ``` ### Response - Success Response (200): Varies based on the HTTP method and endpoint. For `/login`, it returns a welcome message. For `/books`, it returns book data. For `/books/` (PUT), it confirms the update. For `/books/` (DELETE), it removes the book. - Error Response: `KeyNotFound` exception returns a 404 Not Found response. ``` -------------------------------- ### Query Parameters in Routes Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Define routes that accept and process query parameters from the URL. ```APIDOC ## Query Parameters ### Description Routes can be defined to accept and utilize query parameters from the URL. ### Method GET ### Endpoint - `/books` ### Parameters #### Query Parameters - `title` (String) - Optional - The title to filter books by. - `limit` (Number) - Optional - The maximum number of books to return. ### Request Example ```smalltalk Teapot on GET: '/books' -> [:req | books findByTitle: (req at: #title) limit: (req at: #limit) ]; start. "Example matching URL: http://localhost:1701/books?title=smalltalk&limit=12" ``` ### Response - Success Response (200): Returns a list of books matching the criteria. ``` -------------------------------- ### Serving Static Content Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Configure the server to serve static files from a specified directory for a given URL prefix. ```APIDOC ## Serving Static Content ### Description Configures the Teapot server to serve static files from a local directory. ### Method All (Implicit) ### Endpoint - `/statics` (URL prefix) - `/var/www/htdocs` (Local directory path) ### Parameters - `urlPrefix` (String): The URL path that will serve static content. - `directoryPath` (String): The local file system path to the directory containing static files. ### Request Example ```smalltalk Teapot on serveStatic: '/statics' from: '/var/www/htdocs'; start. ``` ### Response Static files are served directly from the specified directory. ``` -------------------------------- ### Use Regex for Routing in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This example demonstrates using regular expressions for defining routes in Teapot. It shows how to capture groups from a URL pattern (e.g., `([a-z]+dd)`) and use them in the handler. The captured values can be accessed using the request object. ```smalltalk Teapot on GET: '/hi/([a-z]+\d\d)' asRegex -> [:req | 'Hello ', (req at: 1)]; start. (ZnEasy get: 'http://localhost:1701/hi/user01') entity string. "Hello user01" ZnEasy get: 'http://localhost:1701/hi/user'. "not found" ``` -------------------------------- ### Implement Route Conditions in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This example demonstrates how to add conditions to Teapot routes and filters. Conditions are boolean expressions evaluated before executing the route handler. The snippets show conditions based on the 'Accept' header and the request method. ```smalltalk Teapot on GET: 'test1' -> result; when: [:req | req accept = 'application/json']; any: 'test2' -> result; when: [:req | #(GET POST) includes: req method]; start. "first one matches only if the accept header is set to application/json" "second one matches if the request method is either GET or POST" ``` -------------------------------- ### Response Transformer - Stream Output Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Configures a route to stream a file as the response. ```APIDOC ## GET /download ### Description Defines a route that streams the content of a file as the response. ### Method GET ### Endpoint /download ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **file content** (stream) - The content of the file. #### Response Example ```text (Binary stream of file content) ``` ``` -------------------------------- ### Response Transformer - Text Output Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Configures a route to output plain text. ```APIDOC ## GET /sometext ### Description Defines a route that returns a simple text string. ### Method GET ### Endpoint /sometext ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **message** (string) - A plain text message. #### Response Example ```text this is text plain ``` ``` -------------------------------- ### Load Teapot Baseline Source: https://github.com/zeroflag/teapot/blob/master/docs/Installation.md Loads the Teapot baseline directly from its GitHub repository. Users can specify a released version instead of 'master' for pinned versions. ```smalltalk Metacello new baseline: 'Teapot'; repository: 'github://zeroflag/teapot:master/source'; load. ``` -------------------------------- ### Handle Multiple URL Patterns in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This code shows how Teapot supports defining multiple URL patterns for a single route or filter. It uses collection literals (arrays) to specify several patterns for 'before' filters and 'GET' routes, allowing for more flexible routing configurations. ```smalltalk Teapot on before: { '/secure/*' . '/protected/*' } -> [ :req | req abort: TeaResponse unauthorized ]; GET: { '/path1/*'. '/path2/\d+' asRegex } -> 'path1 or path2'; start. ``` -------------------------------- ### Serve Static Content with Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This snippet shows how to configure Teapot to serve static files from a specified directory. The 'serveStatic' method maps a URL prefix to a local file system path, making it easy to serve assets like CSS, JavaScript, and images. ```smalltalk Teapot on serveStatic: '/statics' from: '/var/www/htdocs'; start. ``` -------------------------------- ### Route with Parameter Constraint Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Illustrates how to define a route with a parameter constraint, ensuring the parameter matches a specific type. ```APIDOC ## GET /user/ ### Description Defines a route to find a user by their ID, with a constraint that the ID must be an integer. ### Method GET ### Endpoint /user/ ### Parameters #### Path Parameters - **id** (integer) - Required - The ID of the user to find. Must be an integer. #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **user** (object) - The user object if found. #### Response Example ```json { "user": { "id": 123, "name": "John Doe" } } ``` ``` -------------------------------- ### Before Filters Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Before filters are executed before a request is processed, allowing for pre-request checks or modifications. ```APIDOC ## Before Filters ### Description Before filters are evaluated before each request that matches the given URL pattern. ### Method All (Implicit) ### Endpoint - `/secure/*` - `*` ### Parameters None explicitly defined in this section, but the closure receives a `req` object. ### Request Example ```smalltalk Teapot on before: '/secure/*' -> [:req | req session attributeAt: #user ifAbsent: [req abort: (TeaResponse redirect location: '/loginpage')]]; before: '*' -> (Send message: #logRequest: to: auditor); start. ``` ### Response Responses are handled within the filter closures, typically by aborting the request or logging it. ``` -------------------------------- ### Conditions on Routes and Filters Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Apply conditions to routes and filters to control their execution based on request properties. ```APIDOC ## Conditions ### Description Routes and Before/After filters can include conditions, which are Boolean expressions evaluated against the request to determine if the route/filter should be executed. ### Method GET, ANY ### Endpoint - `test1` - `test2` ### Parameters #### Request Parameters (within the condition closure) - `req` (Request object): Used to access request properties like headers and method. ### Request Example ```smalltalk Teapot on GET: 'test1' -> result; when: [:req | req accept = 'application/json']; any: 'test2' -> result; when: [:req | #(GET POST) includes: req method]; start. "Explanation:" "The first route matches only if the Accept header is 'application/json'." "The second route matches if the request method is either GET or POST." ``` ### Response - Success Response (200): Depends on the `result` defined in the route. ``` -------------------------------- ### Aborting a Request - Simple Message Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Shows how to abort a request with a simple string message. ```APIDOC ## GET /unauthorized ### Description Defines a route that aborts the request with a simple string message. ### Method GET ### Endpoint /unauthorized ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Bad Request Response (400) - **message** (string) - A simple message indicating the request should go away. #### Response Example ```text go away ``` ``` -------------------------------- ### After Filters Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md After filters are executed after a request has been processed, allowing for response modifications. ```APIDOC ## After Filters ### Description After filters are evaluated after each request and can read the request and modify the response. ### Method All (Implicit) ### Endpoint - `/*` ### Parameters The closure receives `req` (request) and `resp` (response) objects. ### Request Example ```smalltalk Teapot on after: '/*' -> [:req :resp | resp headers at: 'X-Foo' put: 'set by after filter']; start. ``` ### Response After filters can modify the response object before it is sent to the client. ``` -------------------------------- ### Aborting a Request - Unauthorized Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Demonstrates how to abort a request early by returning an unauthorized response. ```APIDOC ## GET /secure/* ### Description Defines a route that checks for security and aborts the request with an unauthorized response if criteria are not met. ### Method GET ### Endpoint /secure/* ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Unauthorized Response (401) - **error** (string) - Indicates that the request is unauthorized. #### Response Example ```json { "error": "Unauthorized" } ``` ``` -------------------------------- ### Response Transformer - JSON Output Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Configures a route to output a JSON array. ```APIDOC ## GET /jsonlist ### Description Defines a route that returns a list of numbers, transformed into a JSON array. ### Method GET ### Endpoint /jsonlist ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **data** (array) - An array of numbers. #### Response Example ```json { "data": [1, 2, 3, 4] } ``` ``` -------------------------------- ### Configure Before Filters in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This snippet demonstrates how to configure 'before' filters in Teapot. These filters execute before a request is processed and can be used for authentication or logging based on URL patterns. It shows how to check for user sessions and redirect if absent, or log requests. ```smalltalk Teapot on before: '/secure/*' -> [:req | req session attributeAt: #user ifAbsent: [req abort: (TeaResponse redirect location: '/loginpage')]]; before: '*' -> (Send message: #logRequest: to: auditor); GET: '/secure' -> 'protected'; start. ``` -------------------------------- ### Response Transformer - Mustache Template Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Configures a route to render a response using a Mustache HTML template. ```APIDOC ## GET /greet ### Description Defines a route that uses a Mustache template to render an HTML response with dynamic data. ### Method GET ### Endpoint /greet ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this example" } ``` ### Response #### Success Response (200) - **rendered HTML** (string) - The HTML output from the Mustache template. #### Response Example ```html Hello World! ``` ``` -------------------------------- ### Multiple URL Patterns Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Define a single route or filter to match multiple URL patterns. ```APIDOC ## Multiple URL Patterns ### Description Teapot supports defining multiple URL patterns for a single route or filter, allowing for more flexible routing configurations. ### Method - `before` (Implicit) - `GET` ### Endpoint #### Before Filter Patterns - `{ '/secure/*' . '/protected/*' }` #### GET Patterns - `{ '/path1/*' . '/path2/\d+' asRegex }` ### Parameters None directly in the pattern definition, but closures receive `req`. ### Request Example ```smalltalk Teapot on before: { '/secure/*' . '/protected/*' } -> [ :req | req abort: TeaResponse unauthorized ]; GET: { '/path1/*' . '/path2/\d+' asRegex } -> 'path1 or path2'; start. ``` ### Response - `before` filter: Responds with unauthorized if URL matches. - `GET` route: Returns 'path1 or path2' if URL matches one of the patterns. ``` -------------------------------- ### Teapot Route with Parameter Constraints Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Sets up a Teapot server with a route that accepts an integer parameter in the URL. It specifies a constraint `IsInteger` to validate and convert the parameter. The `output: #ston` directive sets the default response transformer. ```smalltalk Teapot on GET: '/user/' -> [:req | users findById: (req at: #id)]; output: #ston; start. ``` -------------------------------- ### Reference Teapot as Dependency Source: https://github.com/zeroflag/teapot/blob/master/docs/Installation.md Includes Teapot as a dependency within your project's baseline configuration. This involves specifying the repository and the package to load. ```smalltalk setUpDependencies: spec spec baseline: 'Teapot' with: [ spec repository: 'github://zeroflag/Teapot:v{XX}/source'; loads: #('Deployment') ]; import: 'Teapot'. ``` ```smalltalk baseline: spec baseline> spec for: #common do: [ self setUpDependencies: spec. spec package: 'My-Package' with: [ spec requires: #('Teapot') ] ] ``` -------------------------------- ### Regex Patterns in Routes Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Use regular expressions for matching complex URL patterns and extracting dynamic segments. ```APIDOC ## Regex Patterns ### Description Allows the use of regular expressions in route definitions to match URL patterns dynamically and capture parts of the URL. ### Method GET ### Endpoint - `/hi/([a-z]+\d\d)` ### Parameters - `req` (Request object): Contains captured groups from the regex match. - `req at: 1`: Accesses the first captured group. ### Request Example ```smalltalk Teapot on GET: '/hi/([a-z]+\d\d)' asRegex -> [:req | 'Hello ', (req at: 1)]; start. "Client request example:" (ZnEasy get: 'http://localhost:1701/hi/user01') entity string. "Hello user01" (ZnEasy get: 'http://localhost:1701/hi/user'). "not found" ``` ### Response - Success Response (200): Returns a string based on the regex match and captured groups. ``` -------------------------------- ### Error Handlers Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md Define custom handlers for specific exceptions that may occur during request processing. ```APIDOC ## Error Handlers ### Description Provides a mechanism to define custom handlers for specific exceptions that can occur within routes or before filters. ### Method GET ### Endpoint - `/divide//` - `/at/` ### Parameters - `req` (Request object): Contains parameters extracted from the URL. - `ex` (Exception object): The exception that was caught. ### Request Example ```smalltalk Teapot on GET: '/divide//' -> [:req | (req at: #a) / (req at: #b)]; GET: '/at/' -> [:req | dict at: (req at: #key)]; exception: ZeroDivide -> [:ex :req | TeaResponse badRequest ]; exception: KeyNotFound -> {#result -> 'error'. #code -> 42}; output: #json; start. "Client request examples:" (ZnEasy get: 'http://localhost:1701/div/6/3') entity string. "2" (ZnEasy get: 'http://localhost:1701/div/6/0'). "bad request" ``` ### Response - Success Response (200): Depends on the route handler. - Error Response: Custom responses defined by the `exception:` blocks (e.g., `TeaResponse badRequest`, JSON with error details). ``` -------------------------------- ### Configure Error Handlers in Teapot Source: https://github.com/zeroflag/teapot/blob/master/docs/UserGuide.md This code configures exception handlers for specific error types in Teapot. It shows how to define handlers for 'ZeroDivide' and 'KeyNotFound' exceptions, returning custom responses or JSON objects. It also demonstrates handling multiple exceptions with a comma-separated list. ```smalltalk Teapot on GET: '/divide//' -> [:req | (req at: #a) / (req at: #b)]; GET: '/at/' -> [:req | dict at: (req at: #key)]; exception: ZeroDivide -> [:ex :req | TeaResponse badRequest ]; exception: KeyNotFound -> {#result -> 'error'. #code -> 42}; output: #json; start. (ZnEasy get: 'http://localhost:1701/div/6/3') entity string. "2" (ZnEasy get: 'http://localhost:1701/div/6/0'). "bad request" ``` -------------------------------- ### Load Teapot using Metacello Source: https://github.com/zeroflag/teapot/blob/master/README.md This snippet demonstrates how to load the Teapot framework and its dependencies into a Pharo Smalltalk environment using the Metacello package manager. It specifies the baseline configuration and the GitHub repository for Teapot. ```smalltalk Metacello new baseline: 'Teapot'; repository: 'github://zeroflag/Teapot/source'; load. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.