Documentation

Overview

    Package jhttp implements a bridge from HTTP to JSON-RPC. This permits requests to be submitted to a JSON-RPC server using HTTP as a transport.

    Example
    Output:
    
    {"jsonrpc":"2.0","id":10235,"result":"full plate and packing steel"}
    

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func HTTPRequest

    func HTTPRequest(ctx context.Context) *http.Request

      HTTPRequest returns the HTTP request associated with ctx, or nil. The context passed to the JSON-RPC client by the Bridge will contain this value.

      Types

      type Bridge

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

        A Bridge is a http.Handler that bridges requests to a JSON-RPC client.

        The body of the HTTP POST request must contain the complete JSON-RPC request message, encoded with Content-Type: application/json. Either a single request object or a list of request objects is supported.

        If the request completes, whether or not there is an error, the HTTP response is 200 (OK) for ordinary requests or 204 (No Response) for notifications, and the response body contains the JSON-RPC response.

        If the HTTP request method is not "POST", the bridge reports 405 (Method Not Allowed). If the Content-Type is not application/json, the bridge reports 415 (Unsupported Media Type).

        The bridge attaches the inbound HTTP request to the context passed to the client, allowing an EncodeContext callback to retrieve state from the HTTP headers. Use jhttp.HTTPRequest to retrieve the request from the context.

        func NewBridge

        func NewBridge(c *jrpc2.Client) *Bridge

          NewBridge constructs a new Bridge that dispatches requests through c. It is safe for the caller to continue to use c concurrently with the bridge, as long as it does not close the client.

          func (*Bridge) Close

          func (b *Bridge) Close() error

            Close shuts down the client associated with b and reports the result from its Close method.

            func (*Bridge) ServeHTTP

            func (b *Bridge) ServeHTTP(w http.ResponseWriter, req *http.Request)

              ServeHTTP implements the required method of http.Handler.

              type Channel

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

                A Channel implements an channel.Channel that dispatches requests via HTTP to a user-provided URL. Each message sent to the channel is an HTTP POST request with the message as its body.

                func NewChannel

                func NewChannel(url string, opts *ChannelOptions) *Channel

                  NewChannel constructs a new channel that posts to the specified URL.

                  func (*Channel) Close

                  func (c *Channel) Close() error

                    Close shuts down the channel, discarding any pending responses.

                    func (*Channel) Recv

                    func (c *Channel) Recv() ([]byte, error)

                      Recv receives the next available response and reports its body.

                      func (*Channel) Send

                      func (c *Channel) Send(msg []byte) error

                        Send forwards msg to the server as the body of an HTTP POST request.

                        type ChannelOptions

                        type ChannelOptions struct {
                        	// The HTTP client to use to send requests. If nil, uses http.DefaultClient.
                        	Client HTTPClient
                        }

                          ChannelOptions gives optional parameters for constructing an HTTP channel. A nil *ChannelOptions is ready for use, and provides default options as described.

                          type HTTPClient

                          type HTTPClient interface {
                          	Do(*http.Request) (*http.Response, error)
                          }

                            HTTPClient is the interface to an HTTP client used by a Channel. It is compatible with the standard library http.Client type.