Documentation
¶
Overview ¶
Package json provides utilities for handling JSON requests and responses.
Index ¶
- func ReadBody[T any](req *http.Request) (T, error)
- type Response
- func (r *Response) AcceptedWithMessage(message string)
- func (r *Response) BadRequestWithMessage(message string)
- func (r *Response) BadRequestWithMessages(responseMessage, logMessage string)
- func (r *Response) ConflictWithMessage(message string)
- func (r *Response) ConflictWithMessages(responseMessage, logMessage string)
- func (r *Response) CreatedWithMessage(message string)
- func (r *Response) CreatedWithURI(uri string)
- func (r *Response) CreatedWithURIAndMessage(uri string, message string)
- func (r *Response) Data(status int, data any)
- func (r *Response) ErrorWithMessages(code int, responseMessage string, logMessage string, err error)
- func (r *Response) ForbiddenWithMessage(message string)
- func (r *Response) ForbiddenWithMessages(responseMessage, logMessage string)
- func (r *Response) InternalServerErrorWithMessage(err error, message string)
- func (r *Response) InternalServerErrorWithMessages(err error, responseMessage string, logMessage string)
- func (r *Response) InvalidInputWithMessage(err error, message string)
- func (r *Response) InvalidInputWithMessages(err error, responseMessage, logMessage string)
- func (r *Response) NoContent()
- func (r *Response) NotAcceptableWithMessage(message string)
- func (r *Response) NotAcceptableWithMessages(responseMessage, logMessage string)
- func (r *Response) NotFoundWithMessage(message string)
- func (r *Response) NotFoundWithMessages(responseMessage, logMessage string)
- func (r *Response) OK(data any)
- func (r *Response) UnauthorizedWithMessage(message string)
- func (r *Response) UnauthorizedWithMessages(responseMessage, logMessage string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 NewResponseWithLogger ¶
NewResponseWithLogger creates a new Response helper with a logger that includes request metadata.
func (*Response) AcceptedWithMessage ¶ added in v0.45.5
AcceptedWithMessage sends a 202 Accepted response.
func (*Response) BadRequestWithMessage ¶
BadRequestWithMessage sends a 400 Bad Request response with a JSON error message.
func (*Response) BadRequestWithMessages ¶
BadRequestWithMessages sends a 400 Bad Request response and logs a separate message.
func (*Response) ConflictWithMessage ¶
ConflictWithMessage sends a 409 Conflict response.
func (*Response) ConflictWithMessages ¶
ConflictWithMessages sends a 409 Conflict response and logs a separate message.
func (*Response) CreatedWithMessage ¶
CreatedWithMessage sends a 201 Created response.
func (*Response) CreatedWithURI ¶ added in v0.53.0
CreatedWithURI sends a 201 Created response with a Location header pointing to the newly created resource. The uri parameter should be the full path/URL to the newly created resource. This follows REST best practices by including the Location header and resource URI in the response.
func (*Response) CreatedWithURIAndMessage ¶ added in v0.53.0
CreatedWithURIAndMessage sends a 201 Created response with a Location header and custom message.
func (*Response) ErrorWithMessages ¶
func (r *Response) ErrorWithMessages(code int, responseMessage string, logMessage string, err error)
ErrorWithMessages sends an error response with the specified status code and messages.
func (*Response) ForbiddenWithMessage ¶ added in v0.39.0
ForbiddenWithMessage sends a 403 Forbidden response with the same message for response and log.
func (*Response) ForbiddenWithMessages ¶ added in v0.39.0
ForbiddenWithMessages sends a 403 Forbidden response.
func (*Response) InternalServerErrorWithMessage ¶
InternalServerErrorWithMessage sends a 500 Internal Server Error response.
func (*Response) InternalServerErrorWithMessages ¶
func (r *Response) InternalServerErrorWithMessages(err error, responseMessage string, logMessage string)
InternalServerErrorWithMessages sends a 500 Internal Server Error response and logs the error.
func (*Response) InvalidInputWithMessage ¶
InvalidInputWithMessage sends a 400 Bad Request response for invalid input.
func (*Response) InvalidInputWithMessages ¶
InvalidInputWithMessages sends a 400 Bad Request response for invalid input and logs the error.
func (*Response) NoContent ¶
func (r *Response) NoContent()
NoContent sends a 204 No Content response.
func (*Response) NotAcceptableWithMessage ¶
NotAcceptableWithMessage sends a 406 Not Acceptable response.
func (*Response) NotAcceptableWithMessages ¶
NotAcceptableWithMessages sends a 406 Not Acceptable response and logs a separate message.
func (*Response) NotFoundWithMessage ¶
NotFoundWithMessage sends a 404 Not Found response.
func (*Response) NotFoundWithMessages ¶
NotFoundWithMessages sends a 404 Not Found response and logs a separate message.
func (*Response) UnauthorizedWithMessage ¶ added in v0.39.0
UnauthorizedWithMessage sends a 401 Unauthorized response with the same message for response and log.
func (*Response) UnauthorizedWithMessages ¶ added in v0.39.0
UnauthorizedWithMessages sends a 401 Unauthorized response.