Skip to main content

Request

Retrieving Data

Form Data

Use the context method Context#FormValue(name string) to get form data by field name:

Get form data
// Handler
func(c slim.Context) error {
name := c.FormValue("name")
return c.String(http.StatusOK, name)
}

We use the curl command to simulate a browser sending a form request:

terminal
curl -X POST http://localhost:1324 -d 'name=Joe'
// => Joe

Additionally, we can implement the interface Slim#BindUnmarshaler to complete binding of custom data type values:

Custom timestamp type
type Timestamp time.Time

func (t *Timestamp) UnmarshalParam(src string) error {
ts, err := time.Parse(time.RFC3339, src)
*t = Timestamp(ts)
return err
}

Query Parameters

Use the context method Context#QueryParam(name string) to get query parameters by field name:

Get query parameters
// Handler
func(c slim.Context) error {
name := c.QueryParam("name")
return c.String(http.StatusOK, name)
})

We use the curl command to simulate a browser sending a request:

terminal
curl -X GET http://localhost:1323\?name\=Joe
// => Joe

Similar to form data, we can also complete binding of custom data type values.

Path Parameters

Use the context method Context#PathParam(name string) string to get path parameters by the defined parameter name, example:

Get path parameters
s.GET("/users/:name", func(c slim.Context) error {
name := c.PathParam("name")
return c.String(http.StatusOK, name)
})

We use the curl command to simulate a browser sending a request:

terminal
curl http://localhost:1323/users/Joe
// => Joe

Binding Data

Additionally, we can bind request data to Go structs through Data Binding.

Validating Data

Slim does not have built-in data validation functionality, but we can register a custom validator by setting the Slim instance property Slim#Validator.

The following example uses the https://github.com/go-playground/validator framework for validation:

Using third-party framework
package main

import (
"net/http"
"log"

"github.com/go-playground/validator"
"go-slim.dev/slim"
)

type (
User struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
}

CustomValidator struct {
validator *validator.Validate
}
)

func (cv *CustomValidator) Validate(i interface{}) error {
if err := cv.validator.Struct(i); err != nil {
// Optionally, you could return the error to give each route more control over the status code
return slim.NewHTTPError(http.StatusBadRequest, err.Error())
}
return nil
}

func main() {
s := slim.New()
s.Validator = &CustomValidator{validator: validator.New()}
s.POST("/users", func(c slim.Context) (err error) {
u := new(User)
if err = c.Bind(u); err != nil {
return slim.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = c.Validate(u); err != nil {
return err
}
return c.JSON(http.StatusOK, u)
})
log.Fatal(s.Start(":1323"))
}

Use the curl command to simulate a browser sending a request

terminal
curl -X POST http://localhost:1324/users \
-H 'Content-Type: application/json' \
-d '{"name":"Joe","email":"joe@invalid-domain"}'
{"message":"Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag"}