Routes
Routes are a special type of template that can be navigated to similarly to pages. They are used to create dynamic pages that can be accessed through a URL. Routes can also be used to create API endpoints that can be called from other applications.
Routes only work with application routes feature. This means the application needs to have a route defined and the route template needs to have one defined as well.
Create a template with type ‘route’ to create a route template.
Internal and public routes
There are 2 types of routes: internal and public. Internal routes are only accessible from the internal session, while public routes can be accessed with app users session (public).
These routes add prefixes to the route URL:
- Internal routes - /int/{app-route}/{route}
- Public routes - /pub/{app-route}/{route}
Routing to the template
If we have an application that has route as ‘demo’ and the route template has route defined as ‘my-route’, we can navigate to it with the following URL:
Required headers
For this to work we need to pass the required headers.
- clientCode - the client code of the account
- sessionKey - the session key of the user (when using internal routes and we do not have a b2bKey)
- b2bKey - the b2b key of the application (always pass this when the api returns one as a cookie and we are not sending it as a cookie back)
Helpers
We can access the session similar to the pages
We can also use all the same regular helpers as in the pages:
We can call functions from routes similar to pages:
Parameters
In routes we do not have the .Parameters helper instead we different helpers to fetch specific parameters.
Query parameters
Read incoming query parameters with the .Query helper. This will return the value of the query parameter as a string.
Path parameters
Read the path1, path2, path3, path4 and path5 parameters with the .Param helper. This will return the value of the parameter as a string.
Json
If we expect that the body is send in json format. The result is gjson.Result object that we can use to read the values from the json body.
Body
If the body is anything else than json we can read it as a string with the .RequestBody helper.
Header
We can read the header values with the .Header helper.
Method
We can read the method of the request with the .Method helper.
Set response content type
By default, the content type of the response is set to text/html. We can change it with the .SetContentType helper.
Set response header
We can set the header of the response with the .SetHeader helper.
Respond
Note that as soon as we call respond then rest of the template does not execute. This is useful when we want to return a response immediately without executing the rest of the template.
The first parameter is the status code and the second parameter is the response body. The response body can be any string, for example json string.