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:

  1. Internal routes - /int/{app-route}/{route}
  2. 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:

https://automat-eu10.erply.com/int/demo/my-route

Required headers

For this to work we need to pass the required headers.

  1. clientCode - the client code of the account
  2. sessionKey - the session key of the user (when using internal routes and we do not have a b2bKey)
  3. 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

{{ $session := .Session.ClientCode }}

We can also use all the same regular helpers as in the pages:

{{ add 1 2 }}

We can call functions from routes similar to pages:

{{ .Tools.Func "demo-app-func" }}

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.

{{ $id := .Query "id" }}

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.

{{ $s2 := .Param "path1" }}

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.

{{ $s3 := .RequestJson }}

Body

If the body is anything else than json we can read it as a string with the .RequestBody helper.

{{ $s4 := .RequestBody }}

We can read the header values with the .Header helper.

{{ $s5 := .Header "X-Custom-Header" }}

Method

We can read the method of the request with the .Method helper.

{{ $method := .Method }}

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.

{{ .SetContentType "application/json" }}

Set response header

We can set the header of the response with the .SetHeader helper.

{{ .SetHeader "X-Custom-Header" "value" }}

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.

{{ .Respond 200 `{"data": "Hello World"}` }}