proto

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: 3 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/proto

Protocol Buffers codec for httpx, over google.golang.org/protobuf. Importing the package registers it for application/protobuf, so httpx.Do[*pb.Message] decodes protobuf responses by their Content-Type; proto.Codec encodes request bodies with httpx.EncodeRequest.

Installation

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

Quick Start

//go:build unit

package proto_test

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

	"google.golang.org/protobuf/types/known/wrapperspb"

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

func Example() {
	// A server that reads and answers Protocol Buffers.
	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
		}
		in := new(wrapperspb.StringValue)
		if err := proto.Codec.Decode(data, in); err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		out, err := proto.Codec.Encode(wrapperspb.String("hello, " + in.GetValue()))
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", proto.Codec.ContentType())
		// The local client reads the whole response; write failures end this handler.
		_, _ = w.Write(out)
	}))
	defer srv.Close()

	// Encode the body as protobuf; httpx.Do decodes the response into the
	// message type named by its type parameter.
	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, proto.Codec, wrapperspb.String("Ada")); err != nil {
		fmt.Println(err)
		return
	}
	reply, resp, err := httpx.Do[*wrapperspb.StringValue](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(reply.GetValue())
	// Output: hello, Ada
}

API

  • Codec httpx.Codec — ContentType() is httpx.ContentTypeProtobuf; Encode and Decode take a proto.Message and fail on anything else.
  • Importing the package calls httpx.RegisterCodec(Codec).

Caveats

  • Only 2xx responses are decoded. Check resp.StatusCode, and close every non-nil resp.Body, including when err reports a decoding failure.
  • Decode with a pointer message type (httpx.Do[*pb.Message]): the helpers allocate the message and hand the codec a *pb.Message.
  • The module requires the parent httpx module at a published version; inside this repository the Go workspace supplies it.

References

Documentation

Overview

Package proto provides Protocol Buffers codec support for httpx.

Import this package to register the protobuf codec:

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

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

httpx.EncodeRequest(req, proto.Codec, myProtoMessage)
result, resp, err := httpx.Do[*pb.MyMessage](client, req)

Index

Constants

This section is empty.

Variables

View Source
var Codec httpx.Codec = codec{}

Codec is the protobuf codec singleton. Use this with httpx.EncodeRequest for request body encoding.

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