Documentation
¶
Overview ¶
Package json provides utilities for handling JSON requests and responses.
Index ¶
- func Data(data any) responseOption
- func Header(key, value string) responseOption
- func Location(uri string) responseOption
- func LogErr(err error) responseOption
- func LogMessage(msg string) responseOption
- func PublicMessage(msg string) responseOption
- func ReadBody[T any](req *http.Request) (T, error)
- type Response
- func (r *Response) Accepted(opts ...responseOption)
- func (r *Response) AcceptedWithMessage(message string)deprecated
- func (r *Response) BadRequest(opts ...responseOption)
- func (r *Response) BadRequestWithMessage(message string)deprecated
- func (r *Response) BadRequestWithMessages(responseMessage, logMessage string)deprecated
- func (r *Response) Conflict(opts ...responseOption)
- func (r *Response) ConflictWithMessage(message string)deprecated
- func (r *Response) ConflictWithMessages(responseMessage, logMessage string)deprecated
- func (r *Response) Created(opts ...responseOption)
- func (r *Response) CreatedWithMessage(message string)deprecated
- func (r *Response) CreatedWithURI(uri string)deprecated
- func (r *Response) CreatedWithURIAndMessage(uri string, message string)deprecated
- func (r *Response) Data(status int, data any)
- func (r *Response) Forbidden(opts ...responseOption)
- func (r *Response) ForbiddenWithMessage(message string)deprecated
- func (r *Response) ForbiddenWithMessages(responseMessage, logMessage string)deprecated
- func (r *Response) InternalServerError(opts ...responseOption)
- func (r *Response) InternalServerErrorWithMessage(err error, message string)deprecated
- func (r *Response) InternalServerErrorWithMessages(err error, responseMessage string, logMessage string)deprecated
- func (r *Response) InvalidInput(opts ...responseOption)
- func (r *Response) InvalidInputWithMessage(err error, message string)deprecated
- func (r *Response) InvalidInputWithMessages(err error, responseMessage, logMessage string)deprecated
- func (r *Response) NoContent(opts ...responseOption)
- func (r *Response) NotAcceptable(opts ...responseOption)
- func (r *Response) NotAcceptableWithMessage(message string)deprecated
- func (r *Response) NotAcceptableWithMessages(responseMessage, logMessage string)deprecated
- func (r *Response) NotFound(opts ...responseOption)
- func (r *Response) NotFoundWithMessage(message string)deprecated
- func (r *Response) NotFoundWithMessages(responseMessage, logMessage string)deprecated
- func (r *Response) OK(opts ...responseOption)
- func (r *Response) Unauthorized(opts ...responseOption)
- func (r *Response) UnauthorizedWithMessage(message string)deprecated
- func (r *Response) UnauthorizedWithMessages(responseMessage, logMessage string)deprecated
- func (r *Response) WithStatus(code int, opts ...responseOption)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Data ¶ added in v0.58.0
func Data(data any) responseOption
Data includes structured data in the response.
func Header ¶ added in v0.58.0
func Header(key, value string) responseOption
Header sets a custom response header.
func Location ¶ added in v0.58.0
func Location(uri string) responseOption
Location sets the Location header (typically for 201 Created responses).
func LogErr ¶ added in v0.58.0
func LogErr(err error) responseOption
LogErr includes an underlying error for server-side logging.
func LogMessage ¶ added in v0.58.0
func LogMessage(msg string) responseOption
LogMessage provides a custom message for server logs (separate from public message).
func PublicMessage ¶ added in v0.58.0
func PublicMessage(msg string) responseOption
PublicMessage sets the message sent to the client (overrides default).
Types ¶
type Response ¶
type Response struct {
Writer http.ResponseWriter
// contains filtered or unexported fields
}
Response simplifies sending structured JSON responses and logging errors.
func NewResponse ¶
func NewResponse(w http.ResponseWriter) *Response
NewResponse creates a new Response helper with the provided ResponseWriter.
func NewResponseFromRequest ¶ added in v0.59.0
func NewResponseFromRequest(w http.ResponseWriter, r *http.Request) *Response
NewResponseFromRequest creates a Response that logs using the zerolog logger stored in r's context (injected by request-ID or principal middleware). All context fields (request_id, principal, auth_source, etc.) are automatically included.
Prefer this over NewResponseWithLogger in HTTP handlers.
func NewResponseWithLogger
deprecated
NewResponseWithLogger creates a new Response helper with a logger that includes request metadata.
Deprecated: Use NewResponseFromRequest in HTTP handlers to automatically inherit the request-scoped context logger (carrying request_id, principal, etc.). NewResponseWithLogger remains useful when an explicit logger is needed (e.g. tests).
func (*Response) Accepted ¶ added in v0.58.0
func (r *Response) Accepted(opts ...responseOption)
Accepted sends a 202 Accepted response.
func (*Response) AcceptedWithMessage
deprecated
added in
v0.45.5
func (*Response) BadRequest ¶ added in v0.58.0
func (r *Response) BadRequest(opts ...responseOption)
BadRequest sends a 400 Bad Request response.
func (*Response) BadRequestWithMessage
deprecated
func (*Response) BadRequestWithMessages
deprecated
func (*Response) Conflict ¶ added in v0.58.0
func (r *Response) Conflict(opts ...responseOption)
Conflict sends a 409 Conflict response.
func (*Response) ConflictWithMessage
deprecated
func (*Response) ConflictWithMessages
deprecated
func (*Response) Created ¶ added in v0.58.0
func (r *Response) Created(opts ...responseOption)
Created sends a 201 Created response.
func (*Response) CreatedWithMessage
deprecated
func (*Response) CreatedWithURI
deprecated
added in
v0.53.0
func (*Response) CreatedWithURIAndMessage
deprecated
added in
v0.53.0
func (*Response) Forbidden ¶ added in v0.58.0
func (r *Response) Forbidden(opts ...responseOption)
Forbidden sends a 403 Forbidden response.
func (*Response) ForbiddenWithMessage
deprecated
added in
v0.39.0
func (*Response) ForbiddenWithMessages
deprecated
added in
v0.39.0
func (*Response) InternalServerError ¶ added in v0.58.0
func (r *Response) InternalServerError(opts ...responseOption)
InternalServerError sends a 500 Internal Server Error response.
func (*Response) InternalServerErrorWithMessage
deprecated
func (*Response) InternalServerErrorWithMessages
deprecated
func (*Response) InvalidInput ¶ added in v0.58.0
func (r *Response) InvalidInput(opts ...responseOption)
InvalidInput sends a 400 Bad Request response for invalid input.
func (*Response) InvalidInputWithMessage
deprecated
func (*Response) InvalidInputWithMessages
deprecated
func (*Response) NoContent ¶
func (r *Response) NoContent(opts ...responseOption)
NoContent sends a 204 No Content response.
func (*Response) NotAcceptable ¶ added in v0.58.0
func (r *Response) NotAcceptable(opts ...responseOption)
NotAcceptable sends a 406 Not Acceptable response.
func (*Response) NotAcceptableWithMessage
deprecated
func (*Response) NotAcceptableWithMessages
deprecated
func (*Response) NotFound ¶ added in v0.58.0
func (r *Response) NotFound(opts ...responseOption)
NotFound sends a 404 Not Found response.
func (*Response) NotFoundWithMessage
deprecated
func (*Response) NotFoundWithMessages
deprecated
func (*Response) Unauthorized ¶ added in v0.58.0
func (r *Response) Unauthorized(opts ...responseOption)
Unauthorized sends a 401 Unauthorized response.
func (*Response) UnauthorizedWithMessage
deprecated
added in
v0.39.0
func (*Response) UnauthorizedWithMessages
deprecated
added in
v0.39.0
func (*Response) WithStatus ¶ added in v0.58.0
WithStatus sends a response with a custom status code.