Documentation
¶
Index ¶
- type BindJSONOptions
- type Ctx
- func (c *Ctx) BindJSON(v any, opts ...BindJSONOptions) error
- func (c *Ctx) Context() context.Context
- func (c *Ctx) Finish()
- func (c *Ctx) Get(key any, def ...any) any
- func (c *Ctx) Header(key, value string)
- func (c *Ctx) JSON(v any) error
- func (c *Ctx) Method() string
- func (c *Ctx) Param(name string) string
- func (c *Ctx) Path() string
- func (c *Ctx) Query(key string) string
- func (c *Ctx) Request() *http.Request
- func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request, ps httprouter.Params, route string)
- func (c *Ctx) ResponseWriter() http.ResponseWriter
- func (c *Ctx) Route() string
- func (c *Ctx) Send(status int, contentType string, b []byte) (int, error)
- func (c *Ctx) Set(key, value any) *Ctx
- func (c *Ctx) SetJSONEscapeHTML(escape bool)
- func (c *Ctx) SetRequest(r *http.Request)
- func (c *Ctx) SetResponseWriter(w http.ResponseWriter)
- func (c *Ctx) Status(code int) *Ctx
- func (c *Ctx) StatusCode() int
- func (c *Ctx) String(status int, body string) error
- func (c *Ctx) WroteHeader() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BindJSONOptions ¶
type BindJSONOptions struct {
// WeaklyTypedInput allows common type coercions, e.g., "10" -> 10 for int fields.
WeaklyTypedInput bool
// ErrorUnused when true returns an error for unexpected fields.
ErrorUnused bool
}
BindJSONOptions customizes how BindJSON decodes JSON payloads when binding into structs. All fields are optional and default to false (strict behavior).
type Ctx ¶
type Ctx struct {
// contains filtered or unexported fields
}
Ctx is the request context for goflash handlers and middleware. It wraps the http.ResponseWriter and *http.Request, exposes convenience helpers, and tracks route, status, and response state for each request.
func (*Ctx) BindJSON ¶
func (c *Ctx) BindJSON(v any, opts ...BindJSONOptions) error
BindJSON decodes the request body JSON into v. If v is a pointer to a struct, behavior can be customized using an optional BindJSONOptions parameter. Defaults: strict decoding using encoding/json with DisallowUnknownFields, no type coercion.
func (*Ctx) Finish ¶
func (c *Ctx) Finish()
Finish is a hook for context cleanup after request handling. No-op by default.
func (*Ctx) Get ¶
Get returns a value from the request context by key. If the key is not present (or the stored value is nil), it returns the provided default when given (Get(key, def)), otherwise it returns nil.
func (*Ctx) JSON ¶
JSON writes the provided value as JSON with status code (defaults to 200 if not set).
func (*Ctx) Param ¶
Param returns a path parameter by name. Returns "" if not found. Note: httprouter.Params.ByName returns "" if not found, so this avoids extra allocation.
func (*Ctx) Query ¶
Query returns a query string parameter by key. Returns "" if not found. Note: url.Values.Get returns "" if not found, so this avoids extra allocation.
func (*Ctx) Reset ¶
func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request, ps httprouter.Params, route string)
Reset prepares the context for a new request. Used internally by the framework.
func (*Ctx) ResponseWriter ¶
func (c *Ctx) ResponseWriter() http.ResponseWriter
ResponseWriter returns the underlying http.ResponseWriter.
func (*Ctx) Set ¶
Set stores a value in the request context using the provided key. It replaces the request with a clone that carries the new context.
Note: Prefer using a custom, unexported key type to avoid collisions.
func (*Ctx) SetJSONEscapeHTML ¶
SetJSONEscapeHTML controls whether JSON responses escape HTML characters (default true).
func (*Ctx) SetRequest ¶
SetRequest replaces the underlying *http.Request.
func (*Ctx) SetResponseWriter ¶
func (c *Ctx) SetResponseWriter(w http.ResponseWriter)
SetResponseWriter replaces the underlying http.ResponseWriter.
func (*Ctx) Status ¶
Status sets the response status code (without writing the header yet). Returns the context for chaining.
func (*Ctx) StatusCode ¶
StatusCode returns the status code that will be written (or 200 if not set yet).
func (*Ctx) WroteHeader ¶
WroteHeader reports whether the response header has been written.