jhttp

package
v0.0.61 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2019 License: BSD-3-Clause Imports: 8 Imported by: 0

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
package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"strings"

	"bitbucket.org/creachadair/jrpc2/handler"
	"bitbucket.org/creachadair/jrpc2/jhttp"
	"bitbucket.org/creachadair/jrpc2/server"
)

func main() {
	// Set up a local server to demonstrate the API.
	loc := server.NewLocal(handler.Map{
		"Test": handler.New(func(ctx context.Context, ss ...string) (string, error) {
			return strings.Join(ss, " "), nil
		}),
	}, nil)
	defer loc.Close()

	b := jhttp.NewClientBridge(loc.Client)
	defer b.Close()

	hsrv := httptest.NewServer(b)
	defer hsrv.Close()

	rsp, err := http.Post(hsrv.URL, "application/json", strings.NewReader(`{
  "jsonrpc": "2.0",
  "id": 10235,
  "method": "Test",
  "params": ["full", "plate", "and", "packing", "steel"]
}`))
	if err != nil {
		log.Fatalf("POST request failed: %v", err)
	}
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		log.Fatalf("Reading response body: %v", err)
	}

	fmt.Println(string(body))
}
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

This section is empty.

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).

func NewBridge

func NewBridge(ch channel.Channel, opts *jrpc2.ClientOptions) *Bridge

NewBridge constructs a new Bridge that dispatches requests through a client constructed from the specified channel and options.

func NewClientBridge

func NewClientBridge(cli *jrpc2.Client) *Bridge

NewClientBridge constructs a new Bridge that dispatches through the given 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.

Jump to

Keyboard shortcuts

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