package json

import ""

Installation | Overview | API | Files


$ go get


Package gorilla/rpc/json provides a codec for JSON-RPC over HTTP services.

To register the codec in a RPC server:

import (

func init() {
    s := rpc.NewServer()
    s.RegisterCodec(json.NewCodec(), "application/json")
    // [...]
    http.Handle("/rpc", s)

A codec is tied to a content type. In the example above, the server will use the JSON codec for requests with "application/json" as the value for the "Content-Type" header.

This package follows the JSON-RPC 1.0 specification:

Request format is:

    The name of the method to be invoked, as a string in dotted notation
    as in "Service.Method".
    An array with a single object to pass as argument to the method.
    The request id, a uint. It is used to match the response with the
    request that it is replying to.

Response format is:

    The Object that was returned by the invoked method,
    or null in case there was an error invoking the method.
    An Error object if there was an error invoking the method,
    or null if there was no error.
    The same id as the request it is responding to.

Check the gorilla/rpc documentation for more details:


Package Files

func DecodeClientResponse

func DecodeClientResponse(r io.Reader, reply interface{}) error

DecodeClientResponse decodes the response body of a client request into the interface reply.

func EncodeClientRequest

func EncodeClientRequest(method string, args interface{}) ([]byte, error)

EncodeClientRequest encodes parameters for a JSON-RPC client request.

type Codec

type Codec struct {

Codec creates a CodecRequest to process each request.

func NewCodec

func NewCodec() *Codec

NewCodec returns a new JSON Codec.

func (*Codec) NewRequest

func (c *Codec) NewRequest(r *http.Request) rpc.CodecRequest

NewRequest returns a CodecRequest.

type CodecRequest

type CodecRequest struct {
    // contains filtered or unexported fields

CodecRequest decodes and encodes a single request.

func (*CodecRequest) Method

func (c *CodecRequest) Method() (string, error)

Method returns the RPC method for the current request.

The method uses a dotted notation as in "Service.Method".

func (*CodecRequest) ReadRequest

func (c *CodecRequest) ReadRequest(args interface{}) error

ReadRequest fills the request object for the RPC method.

func (*CodecRequest) WriteError

func (c *CodecRequest) WriteError(w http.ResponseWriter, _ int, err error)

func (*CodecRequest) WriteResponse

func (c *CodecRequest) WriteResponse(w http.ResponseWriter, reply interface{})

WriteResponse encodes the response and writes it to the ResponseWriter.

type Error

type Error struct {
    Data interface{}

An Error is a wrapper for a JSON interface value. It can be used by either a service's handler func to write more complex JSON data to an error field of a server's response, or by a client to read it.

func (*Error) Error

func (e *Error) Error() string