json

package
v0.53.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package json provides utilities for handling JSON requests and responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadBody

func ReadBody[T any](req *http.Request) (T, error)

ReadBody reads and decodes the JSON request body into the specified type. It automatically closes the request body.

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

func NewResponseWithLogger(w http.ResponseWriter, r *http.Request, l zerolog.Logger) *Response

NewResponseWithLogger creates a new Response helper with a logger that includes request metadata.

func (*Response) AcceptedWithMessage added in v0.45.5

func (r *Response) AcceptedWithMessage(message string)

AcceptedWithMessage sends a 202 Accepted response.

func (*Response) BadRequestWithMessage

func (r *Response) BadRequestWithMessage(message string)

BadRequestWithMessage sends a 400 Bad Request response with a JSON error message.

func (*Response) BadRequestWithMessages

func (r *Response) BadRequestWithMessages(responseMessage, logMessage string)

BadRequestWithMessages sends a 400 Bad Request response and logs a separate message.

func (*Response) ConflictWithMessage

func (r *Response) ConflictWithMessage(message string)

ConflictWithMessage sends a 409 Conflict response.

func (*Response) ConflictWithMessages

func (r *Response) ConflictWithMessages(responseMessage, logMessage string)

ConflictWithMessages sends a 409 Conflict response and logs a separate message.

func (*Response) CreatedWithMessage

func (r *Response) CreatedWithMessage(message string)

CreatedWithMessage sends a 201 Created response.

func (*Response) CreatedWithURI added in v0.53.0

func (r *Response) CreatedWithURI(uri string)

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

func (r *Response) CreatedWithURIAndMessage(uri string, message string)

CreatedWithURIAndMessage sends a 201 Created response with a Location header and custom message.

func (*Response) Data

func (r *Response) Data(status int, data any)

Data sends a JSON response with the specified status code and data.

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

func (r *Response) ForbiddenWithMessage(message string)

ForbiddenWithMessage sends a 403 Forbidden response with the same message for response and log.

func (*Response) ForbiddenWithMessages added in v0.39.0

func (r *Response) ForbiddenWithMessages(responseMessage, logMessage string)

ForbiddenWithMessages sends a 403 Forbidden response.

func (*Response) InternalServerErrorWithMessage

func (r *Response) InternalServerErrorWithMessage(err error, message string)

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

func (r *Response) InvalidInputWithMessage(err error, message string)

InvalidInputWithMessage sends a 400 Bad Request response for invalid input.

func (*Response) InvalidInputWithMessages

func (r *Response) InvalidInputWithMessages(err error, responseMessage, logMessage string)

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

func (r *Response) NotAcceptableWithMessage(message string)

NotAcceptableWithMessage sends a 406 Not Acceptable response.

func (*Response) NotAcceptableWithMessages

func (r *Response) NotAcceptableWithMessages(responseMessage, logMessage string)

NotAcceptableWithMessages sends a 406 Not Acceptable response and logs a separate message.

func (*Response) NotFoundWithMessage

func (r *Response) NotFoundWithMessage(message string)

NotFoundWithMessage sends a 404 Not Found response.

func (*Response) NotFoundWithMessages

func (r *Response) NotFoundWithMessages(responseMessage, logMessage string)

NotFoundWithMessages sends a 404 Not Found response and logs a separate message.

func (*Response) OK

func (r *Response) OK(data any)

OK sends a 200 OK response with the provided data.

func (*Response) UnauthorizedWithMessage added in v0.39.0

func (r *Response) UnauthorizedWithMessage(message string)

UnauthorizedWithMessage sends a 401 Unauthorized response with the same message for response and log.

func (*Response) UnauthorizedWithMessages added in v0.39.0

func (r *Response) UnauthorizedWithMessages(responseMessage, logMessage string)

UnauthorizedWithMessages sends a 401 Unauthorized response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL