Skip to content
Lucas Menéndez edited this page Jun 24, 2019 · 1 revision

Registering new route, the server is prepared to listen to requests on the route's path and handling it calling the route's handler. The route is registered calling to the different functions named like the HTTP verbs.

Registering new routes

New routes must be assigned to an existing server. Register new routes calling desired server method according to the chosen HTTP method, passing the desired path to listen and one handler at least. Check out more information about this here;

server.GET("/hello/<string:msg>", req, mid)
server.POST("/hello/<string:msg>", req, mid)

Routes params

Route registration admits dynamic paths to define typed params inside. The params must have the following format: <type:alias>. After parsing params, its possible to access them using context.Params (check how to parse params in the following section). context.Params its a map[string]interface{} with functions like Exists() or Get() that provides secure API.

The supported types are:

Type Example
bool <bool:foo>
int <int:foo>
float <float:foo>
string <string:foo>

Parsing route params

To use the route params into a handler function, it must first be parsed calling context.ParseParams(). Then, all the params will be accessible from context.Params (a map of string and interface{}):

// handling the route '/hello/<string:foo>'
if err := ctx.ParseParams(); err != nil {
	// catch error
}

foo, _ := ctx.Params.Get("foo") // get foo value safely
fmt.Println(foo) // prints path foo item value
fmt.Println(ctx.Params["foo"]) // or get value like a map