Request
Vox’s Request
object is built on top of go’s native net/http.Request
.
Actually, a vox.Request
is embedding a net/http.Request
in it’s struct definitation. So you can access any net/http.Request
’s public fields or methods from a vox.Request
.
For example, you can access a reequest’s HTTP header like this:
func ExampleHandler(ctx *vox.Context, req *vox.Request, res *vox.Respomse) {
fmt.Println("secret from request header: ", req.Header.Get("X-Secret"))
}
Additionally, vox.Request
have some extra fields / methods which net/http.Request
dose not provides.
For example, vox has a JSON
method to decode JSON request body to go values, with additional functionality to check the content-type header from the request. If the content-type header does not start with “application/json” or decode errors, this function will return an error and set the response status code to 406.
func PostJSONHandler(ctx *vox.Context, req *vox.Request, res *vox.Respomse) {
body := map[string]string{}
if err := req.JSON(&body); err != nil {
return // You do not need to set the response's status code for vox have set it.
}
...
}