Bwf/ GuideFrançais
Bwf Guide

Route a request and link to it

Let’s rename the home operation in the project you created during installation. Its URL will stay the same. The compiler will show us which names depend on the operation.

Rename the home operation

In my-site/openapi.yaml, find the GET for /. Change operationId: home to operationId: welcome and save the file. Now run ../bwf/scripts/bwf check from my-site.

The first error points to OnHome in RouteBindings.bend. Rename it to OnWelcome and check again. Now the compiler points to Api.HomeResponse in Controller/Home.bend. Change it to Api.WelcomeResponse and use Api.Welcome200 for the response. Run the check once more. It passes, and the URL is still /.

Django URL dispatcher

Declare an item route

Suppose each item has a numeric ID. Our example reserves 1 through 1000, so we’ll put those bounds on the path parameter in the contract below. Notice the name item on the GET. We’ll use the builder generated from that name in a moment.

paths:
  /items/{id}:
    get:
      operationId: item
      parameters:
        - {name: id, in: path, required: true, schema: {type: integer, minimum: 1, maximum: 1000}}
      responses:
        '200': {description: Item found, content: {text/plain: {schema: {type: string}}}}

Build the link in a view

With the contract imported as Api, read the view below as a link to one particular item. The braces refer to the id passed to the function. Give it item 42 and the address in the rendered anchor is /items/42.

You can call Api.url_item in Bend code too. In BSX, a link uses GET and a form uses its declared method. Add ?page={current_page} to supply the query key page from current_page. When the variable is called page, you can write ?{page}. Pass None{} for an optional key you want to omit.

def item_link(id: Api.ItemId) -> Html.Html:
  <a href={url("/items/{id}")}>View item</a>

See where bad input is rejected

Change the type of the view’s id argument to String. The build fails because the generated builder expects Api.ItemId. Restore the argument type. When you need an ID from a runtime value, call the generated make_… function and handle its result before you build the link.

The build also rejects a path absent from the contract, a missing required argument or a scalar query key given twice. For an incoming request, the router checks the path, the method and the submitted values. It returns 400 for a bad query value on a matched route. The action checks existence and access after decoding.

With the Journal running, try these requests. You should see 200 for the health check, 405 with an Allow header for POST, and 404 for the missing route.

curl -si http://127.0.0.1:18088/healthz
curl -si -X POST http://127.0.0.1:18088/healthz
curl -si http://127.0.0.1:18088/missing-route

Reserve /items/new

If the contract also declares /items/{slug}, the router uses the fixed route for /items/new.

Exclude new from the slug schema so the item link builder can’t produce the new-item page’s address.

name: slug
in: path
required: true
schema:
  type: string
  not:
    enum: [new]

Try it in the Journal

Publish and edit an articleFilter and paginate the feed