httpx

package
v1.0.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpx holds the HTTP plumbing shared by every handler: status-carrying errors, JSON encoding, and the error response shape.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(r *http.Request, dst any) error

Bind decodes the request body into dst and validates it when dst supports it.

func DecodeJSON

func DecodeJSON(r *http.Request, dst any) error

DecodeJSON reads the request body into dst.

Unknown fields are ignored, matching Spring Boot's Jackson defaults (FAIL_ON_UNKNOWN_PROPERTIES is off), so a client sending an extra key gets the same acceptance it got from the Java service.

func StatusOf

func StatusOf(err error) int

StatusOf returns the HTTP status err should be reported with, defaulting to 500 for anything that is not an *Error.

func WriteError

func WriteError(w http.ResponseWriter, r *http.Request, err error)

WriteError renders err as a JSON failure. Anything that is not an *Error becomes a bare 500: the message of an unexpected error may name internal state, so it is logged rather than returned to the caller.

func WriteJSON

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

WriteJSON writes v as JSON with the given status. A nil v (or status 204) writes no body, which is what the delete endpoints need.

Types

type Constraints

type Constraints struct {
	// contains filtered or unexported fields
}

Constraints accumulates field violations, standing in for the jakarta.validation annotations on the Java DTOs. The messages are carried over verbatim so clients see the same text.

func (*Constraints) Add

func (c *Constraints) Add(field, message string)

Add records an arbitrary violation.

func (*Constraints) Email

func (c *Constraints) Email(field, message string, value *string)

Email records a violation when value is present but not plausibly an address, mirroring @Email. Like Jakarta's default, a nil or empty value passes.

func (*Constraints) Fields

func (c *Constraints) Fields() []FieldError

Fields returns everything recorded so far.

func (*Constraints) Nested

func (c *Constraints) Nested(prefix string, v Validator)

Nested folds the violations of a nested payload in under a prefix, mirroring @Valid on a nested DTO.

func (*Constraints) NotBlank

func (c *Constraints) NotBlank(field, message string, value string)

NotBlank records a violation when value is empty or only whitespace, mirroring @NotBlank.

func (*Constraints) NotBlankPtr

func (c *Constraints) NotBlankPtr(field, message string, value *string)

NotBlankPtr is NotBlank for an optional field: a nil pointer is as much a violation as a blank string.

func (*Constraints) NotNil

func (c *Constraints) NotNil(field, message string, value any)

NotNil records a violation when value is nil, mirroring @NotNull.

func (*Constraints) Positive

func (c *Constraints) Positive(field, message string, value *int64)

Positive records a violation when value is nil or not greater than zero, mirroring @NotNull @Positive on wallTimeMinutes.

type Error

type Error struct {
	Status  int
	Message string

	// Fields carries per-field validation failures. Empty for everything else.
	Fields []FieldError

	// Err is an optional wrapped cause, kept out of the response body.
	Err error
}

Error is a failure that already knows its HTTP status. It is the counterpart to Spring's ResponseStatusException: services raise it, and the handler layer turns it into a response without needing to know what went wrong.

func BadRequest

func BadRequest(format string, args ...any) *Error

BadRequest reports a malformed or semantically invalid request.

func Conflict

func Conflict(format string, args ...any) *Error

Conflict reports a request that collides with existing state — the two cases here are deleting an SSH credential still in use and deleting a template that still has deployments.

func Forbidden

func Forbidden(format string, args ...any) *Error

Forbidden reports an authenticated caller lacking the required authority. It corresponds to Spring's AccessDeniedException for a non-anonymous principal.

func Invalid

func Invalid(fields []FieldError) *Error

Invalid reports failed request-body validation.

func NotFound

func NotFound(format string, args ...any) *Error

NotFound reports a missing resource. Message is phrased as the Java services phrase it, e.g. "Cluster not found: abc123".

func Unauthorized

func Unauthorized(format string, args ...any) *Error

Unauthorized reports a missing or unusable token. Spring returns this rather than 403 when the principal is anonymous, because the caller can fix it by presenting credentials.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type FieldError

type FieldError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

FieldError is one failed validation constraint.

type Validator

type Validator interface {
	Validate() []FieldError
}

Validator is implemented by request payloads that carry constraints.

Jump to

Keyboard shortcuts

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