One of the most important object is *neo.Ctx object, which is argument of middleware and route handler’s functions.

Through this object you can access:

  • Request
  • Response
  • Contextual Data
  • Event Bus

Let’s cover all of them.

Request

Request is actually wrapped http.Request. Everything what http.Request contains, neo.Request contains too. You can read about http.Request on official site.

URL Parameters

They are also described in routing tutorial.

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    value := ctx.Req.Params.Get("param")
})

Parsing Json Body

Neo has conviniend method for parsing JSON body. You just need to provide instance of your struct where Neo will put data from JSON Body.

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    parsed := &myType{}
    err := ctx.Req.JsonBody(parsed)
    if err != nil {
    	// handle error
    }
})

Response

Response object is actually set of utility methods for writing to http.ResponseWriter. You can write headers, cookies, and different types of request results.

Headers

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    // getting
    ctx.Res.Header().Get("key")

    // adding new
    ctx.Res.Header().Add("key", "value")
    ctx.Res.Header().Set("key", "value")

    // removing
    ctx.Res.Header().Del("key")
})

Explanations what particual header method does you can read here.

Cookies

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    // getting
    ctx.Res.Cookie.Get("key")

    // adding new
    ctx.Res.Cookie.SetCustom(&http.Cookie{/* set fields */})
    ctx.Res.Cookie.Set("key", "value")

    // removing
    ctx.Res.Cookie.Del("key")
})

Sending responses

Neo provides API for sending Json, Xml, File, Text, Raw Data or Templates as responses to client requests.

Json

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    data := Person{"Some", "Person"}
    return 200, ctx.Res.Json(data)
})

Xml

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    data := Person{"Some", "Person"}
    return 200, ctx.Res.Xml(data)
})

File

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    return 200, ctx.Res.File("/path/to/file/to/send")
})

Text

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    return 200, ctx.Res.Text("some text")
})

Raw Data

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    data := []byte("my raw data")
    return 200, ctx.Res.Raw(data)
})

Binary

app.Get("/", func(ctx *neo.Ctx) (int, error) {
	f, _ := os.Open("./some/existing/file.txt")
	_, err := io.Copy(c.Res, f)
	return 200, err
})

Template

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    data := Person{"Some", "Person"}
    return 200, ctx.Res.Tpl("templateName", data)
})

Contextual Data

Neo allows you to put some contextual data into request. All middlewares and route handler will be able to read those value, but also they are able to put or remove values to/from Context map.

app.Use("/", func(ctx *neo.Ctx, next neo.Next) {
    ctx.Data.Set("session", someValue)
    next()
})

And later you can get value from Context.

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    session := ctx.Data.Get("session")
})

Removing values from Context is simple too:

app.Get("/", func(ctx *neo.Ctx) (int, error) {
    session := ctx.Data.Del("session")
})

Event Bus

You can listen for events on neo.Ctx instance. You can for example emit event in one middleware, and listen for it on another middleware, and similar.

You can read more about events in events section.