http - sending JSON with go -


i'm trying send json message go. server code:

func (network *network) join(     w http.responsewriter,     r *http.request) {     //the request not interesting     //the response message clientid value set     log.println("client wants join")     message := message{-1, -1, -1, clientid(len(network.clients)), -1, -1}     var buffer bytes.buffer     enc := json.newencoder(&buffer)      err := enc.encode(message)     if err != nil {         fmt.println("error encoding response join request")         log.fatal(err)     }      fmt.printf("the json: %s\n", buffer.bytes())     fmt.fprint(w, buffer.bytes()) } 

network custom struct. in main function, i'm creating network object , registering it's methods callbacks http.handlefunc(...)

func main() {     runtime.gomaxprocs(2)     var network = new(network)     var clients = make([]client, 0, 10)     network.clients = clients      log.println("starting server")     http.handlefunc("/request", network.request)     http.handlefunc("/update", network.getnews)     http.handlefunc("/join", network.join)     log.fatal(http.listenandserve("localhost:5000", nil)) } 

message struct, too. has 6 fields of type alias int. when client sends http request url "localhost:5000/join", should happen

  • the method join on network object called
  • a new message object id client created
  • this message encoded json
  • to check if encoding correct, encoded message printed on cmd
  • the message written responsewriter

the client rather simple. has exact same code message struct. in main function sends request "localhost:5000/join" , tries decode response. here's code

func main() {      // try join     var clientid clientid     start := time.now()     var message message     resp, err := http.get("http://localhost:5000/join")     if err != nil {         log.fatal(err)     }      fmt.println(resp.status)     dec := json.newdecoder(resp.body)     err = dec.decode(&message)     if err != nil {         fmt.println("error decoding response join request")         log.fatal(err)     }      fmt.println(message)     duration := time.since(start)     fmt.println("connected after: ", duration)     fmt.println("with clientid", message.clientid) } 

i've started server, waited few seconds , ran client. result

  • the server prints "client wants join"
  • the server prints "the json: {"what":-1,"tag":-1,"id":-1,"clientid":0,"x":-1,"y":-1}"
  • the client prints "200 ok"
  • the client crashes "error decoding response join request"
  • the error "invalid character "3" after array element"

this error message confused me. after all, in json, there's number 3. imported io/ioutil on client , printed response code

b, _ := ioutil.readall(resp.body) fmt.printf("the json: %s\n", b) 

please note print statement same on server. expected see encoded json. instead got this

  • "200 ok"
  • "the json: [123 34 87 104 97 116 ....]" list went on long time

i'm new go , don't know if did correctly. seems if above code printed slice of bytes. strange, on server output converted string.

my guess somehow i'm reading wrong data or message corrupted on way between server , client. these wild guesses.

in server, instead of

fmt.fprint(w, buffer.bytes()) 

you need use:

w.write(buffer.bytes()) 

the fmt package format bytes() go-syntax-like slice bytes represented decimals, so:

[123 34 87 104 97 116 ... etc 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -