• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package main
2
3import (
4	"bytes"
5	"fmt"
6	"io/ioutil"
7	"net/http"
8
9	"echo/hero"
10	"echo/net"
11
12	flatbuffers "github.com/google/flatbuffers/go"
13)
14
15func RequestBody() *bytes.Reader {
16	b := flatbuffers.NewBuilder(0)
17	r := net.RequestT{Player: &hero.WarriorT{Name: "Krull", Hp: 100}}
18	b.Finish(r.Pack(b))
19	return bytes.NewReader(b.FinishedBytes())
20}
21
22func ReadResponse(r *http.Response) {
23	body, err := ioutil.ReadAll(r.Body)
24	if err != nil {
25		fmt.Printf("Unable to read request body: %v\n", err)
26		return
27	}
28
29	res := net.GetRootAsResponse(body, 0)
30	player := res.Player(nil)
31
32	fmt.Printf("Got response (name: %v, hp: %v)\n", string(player.Name()), player.Hp())
33}
34
35func main() {
36	body := RequestBody()
37	req, err := http.NewRequest("POST", "http://localhost:8080/echo", body)
38	if err != nil {
39		fmt.Println(err)
40		return
41	}
42
43	client := http.DefaultClient
44	resp, err := client.Do(req)
45	if err != nil {
46		fmt.Println(err)
47		return
48	}
49
50	ReadResponse(resp)
51}
52