json

package
v0.61.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: Apache-2.0 Imports: 4 Imported by: 1

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 Data added in v0.58.0

func Data(data any) responseOption

Data includes structured data in the response.

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).

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 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

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.

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 (r *Response) AcceptedWithMessage(message string)

Deprecated: Use Accepted() with options instead.

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 (r *Response) BadRequestWithMessage(message string)

Deprecated: Use BadRequest() with options instead.

func (*Response) BadRequestWithMessages deprecated

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

Deprecated: Use BadRequest() with options instead.

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 (r *Response) ConflictWithMessage(message string)

Deprecated: Use Conflict() with options instead.

func (*Response) ConflictWithMessages deprecated

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

Deprecated: Use Conflict() with options instead.

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 (r *Response) CreatedWithMessage(message string)

Deprecated: Use Created() with options instead.

func (*Response) CreatedWithURI deprecated added in v0.53.0

func (r *Response) CreatedWithURI(uri string)

Deprecated: Use Created() with Location() option instead.

func (*Response) CreatedWithURIAndMessage deprecated added in v0.53.0

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

Deprecated: Use Created() with Location() and PublicMessage() options instead.

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) 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 (r *Response) ForbiddenWithMessage(message string)

Deprecated: Use Forbidden() with options instead.

func (*Response) ForbiddenWithMessages deprecated added in v0.39.0

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

Deprecated: Use Forbidden() with options instead.

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 (r *Response) InternalServerErrorWithMessage(err error, message string)

Deprecated: Use InternalServerError() with options instead.

func (*Response) InternalServerErrorWithMessages deprecated

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

Deprecated: Use InternalServerError() with options instead.

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 (r *Response) InvalidInputWithMessage(err error, message string)

Deprecated: Use InvalidInput() with options instead.

func (*Response) InvalidInputWithMessages deprecated

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

Deprecated: Use InvalidInput() with options instead.

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 (r *Response) NotAcceptableWithMessage(message string)

Deprecated: Use NotAcceptable() with options instead.

func (*Response) NotAcceptableWithMessages deprecated

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

Deprecated: Use NotAcceptable() with options instead.

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 (r *Response) NotFoundWithMessage(message string)

Deprecated: Use NotFound() with options instead.

func (*Response) NotFoundWithMessages deprecated

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

Deprecated: Use NotFound() with options instead.

func (*Response) OK

func (r *Response) OK(opts ...responseOption)

OK sends a 200 OK response.

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 (r *Response) UnauthorizedWithMessage(message string)

Deprecated: Use Unauthorized() with options instead.

func (*Response) UnauthorizedWithMessages deprecated added in v0.39.0

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

Deprecated: Use Unauthorized() with options instead.

func (*Response) WithStatus added in v0.58.0

func (r *Response) WithStatus(code int, opts ...responseOption)

WithStatus sends a response with a custom status code.

Jump to

Keyboard shortcuts

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