msgpack

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 10 Imported by: ? 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.

README

httpx/codec/msgpack

MessagePack codecs for httpx, using github.com/vmihailenco/msgpack/v5. The default Codec carries the API's JSON data model in MessagePack; NativeCodec explicitly selects native MessagePack semantics. Importing the package registers the default for application/msgpack typed responses, and httpx.EncodeRequest accepts either codec for requests.

Installation

go get github.com/ashtonian/pkg/httpx/codec/msgpack

Requires Go 1.26 or later.

Quick Start

//go:build unit

package msgpack_test

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

	"github.com/ashtonian/pkg/httpx"
	"github.com/ashtonian/pkg/httpx/codec/msgpack"
)

type User struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func Example() {
	// A server that reads and answers MessagePack.
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		data, err := httpx.ReadAtMost(r.Body, 1<<20)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		var in User
		if err := msgpack.Codec.Decode(data, &in); err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		out, err := msgpack.Codec.Encode(User{ID: in.ID, Name: in.Name + " (saved)"})
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", msgpack.Codec.ContentType())
		// The local client reads the whole response; write failures end this handler.
		_, _ = w.Write(out)
	}))
	defer srv.Close()

	// Encode the body as MessagePack; httpx.Do decodes the response by its
	// Content-Type, which importing the package registered.
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := httpx.EncodeRequest(req, msgpack.Codec, User{ID: 1, Name: "Ada"}); err != nil {
		fmt.Println(err)
		return
	}
	user, resp, err := httpx.Do[User](srv.Client(), req)
	if resp != nil {
		defer resp.Body.Close() // Also close bodies returned alongside decode errors.
	}
	if err != nil {
		fmt.Println(err)
		return
	}
	if resp.StatusCode != http.StatusOK {
		fmt.Println("unexpected status:", resp.Status)
		return
	}
	fmt.Println(resp.StatusCode, user.Name)
	// Output: 200 Ada (saved)
}

Configuration

Selection Type Default Description
Codec httpx.Codec Registered on import JSON tags, field selection, omission rules and JSON/text marshal methods
NativeCodec httpx.Codec Explicit opt-in Native MessagePack tags, binary values, extensions and marshal methods
httpx.WithMaxResponseBytes(n) int64 decode option 10 MiB Bounds a typed helper's response read; direct codec calls receive the caller's byte slice

To use the native model for all typed MessagePack responses, call httpx.RegisterCodec(msgpack.NativeCodec) at startup. Both codecs use application/msgpack; endpoints must agree on the data model. Direct calls to Codec or NativeCodec do not depend on which one is registered.

Representation contract

Codec uses encoding/json for struct selection and typed values. json:"-", json:"-,", embedded fields, omitempty, omitzero, pointers, maps and slices have the same meaning as in JSON. msgpack tags do not override that model. Byte slices become base64 strings; UUIDs, timestamps and other custom JSON/text values use their JSON form. Decoding invokes the target's JSON unmarshaler.

Signed and unsigned 64-bit integers retain their full precision. JSON integer literals outside those ranges are rejected; other JSON numbers use finite float64. Nonfinite numbers, non-string map keys, invalid UTF-8, unsupported extensions, malformed/trailing data and nesting beyond JSON's 10,000-level bound are rejected. Native binary and timestamp values decode to their JSON string forms; native extension callbacks are not invoked by the default codec.

Encoding and decoding materialize a JSON document and intermediate values. They buffer complete representations and are not streaming operations. NativeCodec delegates directly to the native library and supports its tags, binary/time values and custom extensions without JSON transcoding.

API / Interfaces

  • Codec httpx.Codec — JSON-model Encode, Decode and ContentType.
  • NativeCodec httpx.Codec — native msgpack.Marshal / msgpack.Unmarshal and the same content type.
  • Importing the package calls httpx.RegisterCodec(Codec).

Build / Run / Test

The quick start above is mirrored in example_test.go. From this module:

go test -tags=unit -race .
go test -tags=integration -race .

The repository's make httpx-release-check also tests downloaded module archives and examples outside the workspace.

Operations

The codecs hold no connections, goroutines, health endpoints or metrics. Encoding/decoding failures return to the caller. For mismatched fields, check the selected JSON/native model and both endpoints' tags. For malformed or unsupported values, inspect the representation under the application's privacy policy. For ErrResponseTooLarge, deliberately change the typed helper's bound or use a streaming protocol.

Typed helpers decode successful representations by content type. Check resp.StatusCode and close every non-nil response body, including after a decode error; see body ownership.

Security

The default follows JSON's field visibility and custom marshaler contract. NativeCodec does not enforce json:"-": a native peer must use appropriate msgpack tags and its own exposure policy. Choose the model explicitly before sharing API structs with a native protocol. Apply response-size limits before decoding untrusted bodies; direct codec calls impose no separate byte limit.

External Dependencies

  • github.com/ashtonian/pkg/httpx — codec interface, registration and typed helpers, required at a published version; the repository workspace supplies local source during development.
  • github.com/vmihailenco/msgpack/v5 — MessagePack wire encoding and native codec behavior. Versions are pinned in go.mod.

Code Map

File Responsibility
msgpack.go Codec, NativeCodec and default registration
jsonmodel.go JSON-model transcoding and wire validation

References

Documentation

Overview

Package msgpack provides MessagePack codec support for httpx.

Import this package to register the msgpack codec:

import _ "github.com/ashtonian/pkg/httpx/codec/msgpack"

Once imported, httpx's typed request helpers can decode MessagePack responses by Content-Type. Ordinary http.Client.Do still returns bytes. To encode request bodies as msgpack, use httpx.EncodeRequest:

httpx.EncodeRequest(req, msgpack.Codec, myValue)
result, resp, err := httpx.Do[MyType](client, req)

Index

Constants

This section is empty.

Variables

View Source
var Codec httpx.Codec = jsonCodec{}

Codec encodes the JSON data model as MessagePack. JSON field selection, tags and marshal methods apply in both directions; bytes and text values have their JSON representation. Use NativeCodec for native MessagePack.

View Source
var NativeCodec httpx.Codec = nativeCodec{}

NativeCodec uses msgpack tags, binary values, timestamp extensions and MessagePack marshal methods. It does not honor json:"-". It is not registered automatically; use it only with a matching native peer.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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