gin 

Send to Kindle
home » snippets » go » libraries » gin



The Context object

type Context struct {
    Request   *http.Request
    Writer    ResponseWriter
    Keys      map[string]interface{}
    Errors    errorMsgs
    Params    httprouter.Params
    Engine    *Engine
}

// Methods:

// Next should be used only in the middlewares.
// It executes the pending handlers in the chain inside the calling handler.
// See example in github.
func Next()

// Forces the system to do not continue calling the pending handlers in the chain.
func Abort()

// Same than AbortWithStatus() but also writes the specified response status code.
// For example, the first handler checks if the request is authorized. If it's
// not, context.AbortWithStatus(401) should be called.
func AbortWithStatus(code int)

// Fail is the same as Abort plus an error message.
// Calling `context.Fail(500, err)` is equivalent to:
// ```
// context.Error("Operation aborted", err)
// context.AbortWithStatus(500)
// ```
func Fail(code int, err error)

// Attaches an error to the current context. The error is pushed to a list of errors.
// It's a good idea to call Error for each error that occurred during the resolution of a request.
// A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.
func Error(err error, meta interface{})

func*Context) LastError() error

// Sets a new pair key/value just for the specified context.
// It also lazy initializes the hashmap.
func*Context) Set(key string, item interface{})

// Get returns the value for the given key or an error if the key does not exist.
func*Context) Get(key string) (interface{}, error)

// MustGet returns the value for the given key or panics if the value doesn't exist.
func*Context) MustGet(key string) interface{}

// the ForwardedFor middleware unwraps the X-Forwarded-For headers, be careful to only use this
// middleware if you've got servers in front of this server. The list with (known) proxies and
// local ips are being filtered out of the forwarded for list, giving the last not local ip being
// the real client ip.
func ForwardedFor(proxies ...interface{}) HandlerFunc

func ClientIP() string { return c.Request.RemoteAddr }

// This function checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
// "application/json" --> JSON binding
// "application/xml"  --> XML binding
// else --> returns an error
// if Parses the request's body as JSON if Content-Type == "application/json"
// using JSON or XML  as a JSON input. It decodes the json payload into the
// struct specified as a pointer.Like ParseBody() but this method also writes a
// 400 error if the json is not valid.
func Bind(obj interface{}) bool
func BindWith(obj interface{}, b binding.Binding) bool

func Render(code int, render render.Render, obj ...interface{})

// Serializes the given struct as JSON into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/json".
func JSON(code int, obj interface{})

// Serializes the given struct as XML into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/xml".
func XML(code int, obj interface{})

// Renders the HTTP template specified by its file name.
// It also updates the HTTP code and sets the Content-Type as "text/html".
// See http://golang.org/doc/articles/wiki/
func HTML(code int, name string, obj interface{})

// Writes the given string into the response body and sets the Content-Type to "text/plain".
func String(code int, format string, values ...interface{})

// Returns a HTTP redirect to the specific location.
func Redirect(code int, location string)

// Writes some data into the body stream and updates the HTTP code.
func Data(code int, contentType string, data []byte)

// Writes the specified file into the body stream
func File(filepath string)


type Negotiate struct {
    Offered  []string
    HTMLPath string
    HTMLData interface{}
    JSONData interface{}
    XMLData  interface{}
    Data     interface{}
}

func Negotiate(code int, config Negotiate)
func NegotiateFormat(offered ...string) string
func SetAccepted(formats ...string)