utils

package
v0.0.0-...-8699099 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseJSON

func ParseJSON(r *http.Request, payload any) error

ParseJSON decodes the JSON request body into the provided payload. Returns an error if the request body is nil or the JSON is malformed.

func ReadJSON

func ReadJSON(r *http.Request) ([]byte, error)

ReadJSON reads and returns the JSON data from the request body. It returns the JSON data as a byte slice and an error if any occur. It disallows unknown fields and limits the payload size to 1 MB.

func WriteError

func WriteError(w http.ResponseWriter, status int, err error)

WriteError is a helper function to write an error response in JSON format. It sets the appropriate HTTP status code and provides a standardized error message.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any) error

WriteJSON encodes the provided value into JSON and writes it to the response. It sets the response content type to 'application/json' and the given HTTP status code. Returns an error if JSON encoding fails.

Types

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse defines the structure for error messages returned to the client.

Jump to

Keyboard shortcuts

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