httputil

package
v0.0.0-...-ba2e97a Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON[T any](r *http.Request) (T, error)

DecodeJSON decodes the JSON request body into T. Returns a descriptive error for EOF, syntax errors, and type mismatches that callers can map to a 400 response.

func PathParam

func PathParam(r *http.Request, key string) string

PathParam returns the named path parameter from the Go 1.22 ServeMux. Equivalent to r.PathValue(key).

func QueryParam

func QueryParam(r *http.Request, key string) string

QueryParam returns a single query string value.

func WriteError

func WriteError(w http.ResponseWriter, status int, code, message string)

WriteError writes a JSON error body { "code": ..., "message": ... }.

func WriteJSON

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

WriteJSON encodes v as JSON and writes it with the given status code. It encodes into a buffer first so that a marshal error does not leave a partial response body behind.

func WriteNoContent

func WriteNoContent(w http.ResponseWriter)

WriteNoContent writes a 204 No Content response with no body.

Types

type ErrorBody

type ErrorBody struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorBody is the standard error envelope written by WriteError.

Jump to

Keyboard shortcuts

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