Documentation
¶
Overview ¶
Package jsonapi provides utilities for working with json REST APIs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteError ¶
func WriteError(rw http.ResponseWriter, err ErrorResponse, status int)
WriteError writes an ErrorResponse in JSON format to the http.ResponseWriter. It sets the appropriate HTTP status code and content type.
func WriteErrorMsg ¶
func WriteErrorMsg(rw http.ResponseWriter, msg string, status int)
WriteErrorMsg writes an error message in JSON format to the http.ResponseWriter using WriteErrror.
Types ¶
type Endpoint ¶
type Endpoint[Req, Resp any] struct{}
Endpoint represents a JSON API endpoint with a request and response type. It provides methods to parse the request from an io.Reader and write the response to an io.Writer. If an error occurs during parsing or writing, it can write an error response in JSON format using the WriteError method. It is primarily intended to identify and document JSON API endpoints.
func (Endpoint[Req, Resp]) ParseRequest ¶
ParseRequest reads the request body from the provided http.Request and decodes it into the Request field of the Endpoint. If decoding failes, it uses WriteError to write an error message to the client.
func (Endpoint[Req, Resp]) WriteResponse ¶
func (ep Endpoint[Req, Resp]) WriteResponse(rw http.ResponseWriter, resp Resp) error
WriteResponse writes the response in JSON format to the http.ResponseWriter. It sets the Content-Type header to "application/json" and writes the HTTP status code. If encoding the response fails, it uses WriteError to write an error message to the client.
type ErrorResponse ¶
type ErrorResponse struct {
Message string `json:"message"`
}
ErrorResponse represents a JSON error response.