json - golang http+jsonrpc access from web page -


i've used go's net/rpc , net/rpc/jsonrpc packages bit perform connections between go processes, i'm wondering if there way connect http jsonrpc server using server tooling standard library (not have problem writing own, don't want if don't have to).

this basic server setup have:

arith := new(server.arith)  server := rpc.newserver() server.register(arith)  server.handlehttp(rpc.defaultrpcpath, rpc.defaultdebugpath)  listener, e := net.listen("tcp", ":4321") if e != nil {     log.fatal("listen error:", e) } defer listener.close()  http.serve(listener, http.defaultservemux) 

and i'd able hitting web page or simple command line curl call - regular post.

however, line: http://golang.org/src/net/rpc/server.go?s=20445:20475#l670 appears indicate expects http client issue connect , directly write json rpc request stream , receive reply same way. don't know if possible browser, not common or compatible simple post.

is there way start json rpc server can post using ol' xmlhttprequest ?

edit: crap - above not using jsonrpc stuff - trying use gob, whatever - problem same - code in src/net/rpc/server.go not going handle posts, route overall isn't going work regardless of server codec.

fwiw, got working making simple http handler adapts http request/response servercodec. seems work charm.

here's working code test:

import (     "bytes"     "fmt"     "io"     "io/ioutil"     "log"     "net"     "net/http"     "net/rpc"     "net/rpc/jsonrpc"     "testing" )  // adapt http connection readwritecloser type httpconn struct {     in  io.reader     out io.writer }  func (c *httpconn) read(p []byte) (n int, err error)  { return c.in.read(p) } func (c *httpconn) write(d []byte) (n int, err error) { return c.out.write(d) } func (c *httpconn) close() error                      { return nil }  // our service type cakebaker struct{}  func (cb *cakebaker) bakeit(n int, msg *string) error {     *msg = fmt.sprintf("your cake has been bacon (%d)", n)     return nil }  func testhttpserver(t *testing.t) {      fmt.printf("testhttpserver\n")      cb := &cakebaker{}      server := rpc.newserver()     server.register(cb)      listener, e := net.listen("tcp", ":4321")     if e != nil {         log.fatal("listen error:", e)     }     defer listener.close()      go http.serve(listener, http.handlerfunc(func(w http.responsewriter, r *http.request) {          if r.url.path == "/bake-me-a-cake" {             servercodec := jsonrpc.newservercodec(&httpconn{in: r.body, out: w})             w.header().set("content-type", "application/json")             w.writeheader(200)             err := server.serverequest(servercodec)             if err != nil {                 log.printf("error while serving json request: %v", err)                 http.error(w, "error while serving json request, details have been logged.", 500)                 return             }         }      }))      resp, err := http.post("http://localhost:4321/bake-me-a-cake", "application/json", bytes.newbufferstring(         `{"jsonrpc":"2.0","id":1,"method":"cakebaker.bakeit","params":[10]}`,     ))     if err != nil {         panic(err)     }     defer resp.body.close()     b, err := ioutil.readall(resp.body)     if err != nil {         panic(err)     }      fmt.printf("returned json: %s\n", string(b))  } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -