httpjson

package
v0.0.0-...-f6c3f94 Latest Latest
Warning

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

Go to latest
Published: May 23, 2021 License: BSD-2-Clause, BSD-3-Clause, HPND Imports: 8 Imported by: 0

Documentation

Overview

Package httpjson helps transporting JSON over HTTP.

This might get extracted to a standalone repository, if it proves useful enough.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServePOST

func ServePOST(fn interface{}) http.Handler

ServePOST adapts a function to a http.Handler with easy JSON unmarshal & marshal and error reporting.

fn is expected to be of the form

func(context.Context, T1) (T2, error)

or similar with pointers to T1 or T2.

Types

type Resource

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

Resource represents a JSON-speaking remote HTTP resource.

func JSON

func JSON(client *http.Client, url string) *Resource

JSON helps make a HTTP request to the resource tree at url with JSON request and response bodies.

If client is nil http.DefaultClient will be used.

func (*Resource) Call

func (c *Resource) Call(ctx context.Context, data interface{}, dst interface{}) error

Call a HTTP resource that is expected to return JSON data.

If data is not nil, method is POST and the request body is data marshaled into JSON. If data is nil, method is GET.

The response JSON is unmarshaled into dst.

type TrailingDataError

type TrailingDataError struct {
	Token json.Token
}

TrailingDataError is an error that is returned if there is trailing data after a JSON message.

func (*TrailingDataError) Error

func (t *TrailingDataError) Error() string

Jump to

Keyboard shortcuts

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