Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonmw

package
v4.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package jsonmw provides stable JSON request helpers and middleware.

Use StrictDecoder for handlers that should reject malformed JSON and rely on a clear content-type contract. The middleware package is part of the stable core surface and should stay provider-neutral.

Purpose: See the package summary above. Import: `github.com/aatuh/api-toolkit/v4/middleware/json`. Example: See docs/api-reference.md for package example links and docs/cookbook.md for task recipes. Errors: Constructors, parsers, and handlers return or write documented errors according to their signatures; packages with plain data types do not add hidden error channels. Concurrency: Treat configured middleware and helpers as immutable after construction; request and response values remain request-scoped unless a type documents stronger guarantees. Stability: Stable core API under VERSIONING.md and scripts/apicheck.sh. When not to use: Prefer net/http, application-owned types, or narrower helpers when this package contract is not needed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StrictDecoder

func StrictDecoder(r *http.Request) (*json.Decoder, error)

StrictDecoder creates a JSON decoder that disallows unknown fields.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"

	jsonmw "github.com/aatuh/api-toolkit/v4/middleware/json"
)

func main() {
	req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/widgets", strings.NewReader(`{"name":"starter"}`))

	decoder, err := jsonmw.StrictDecoder(req)
	if err != nil {
		panic(err)
	}
	var payload struct {
		Name string `json:"name"`
	}
	if err := decoder.Decode(&payload); err != nil {
		panic(err)
	}

	fmt.Println(payload.Name)

}
Output:
starter

Types

type Middleware

type Middleware struct {
	RequireJSON bool
}

Middleware enforces JSON content-type requirements.

func New

func New(opts Options) (*Middleware, error)

New constructs a JSON middleware with the given requirement.

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler wraps the next handler with JSON content checks.

func (*Middleware) Middleware

func (m *Middleware) Middleware() func(http.Handler) http.Handler

Middleware implements ports.Middleware by returning the Handler adapter.

type Options

type Options struct {
	RequireJSON bool
}

Options configures the JSON middleware.

Jump to

Keyboard shortcuts

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