File: client.go

package info (click to toggle)
golang-github-google-flatbuffers 24.12.23-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 17,704 kB
  • sloc: cpp: 53,217; python: 6,900; cs: 5,566; java: 4,370; php: 1,460; javascript: 1,061; xml: 1,016; sh: 886; makefile: 13
file content (51 lines) | stat: -rw-r--r-- 949 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

	"echo/hero"
	"echo/net"

	flatbuffers "github.com/google/flatbuffers/go"
)

func RequestBody() *bytes.Reader {
	b := flatbuffers.NewBuilder(0)
	r := net.RequestT{Player: &hero.WarriorT{Name: "Krull", Hp: 100}}
	b.Finish(r.Pack(b))
	return bytes.NewReader(b.FinishedBytes())
}

func ReadResponse(r *http.Response) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("Unable to read request body: %v\n", err)
		return
	}

	res := net.GetRootAsResponse(body, 0)
	player := res.Player(nil)
	
	fmt.Printf("Got response (name: %v, hp: %v)\n", string(player.Name()), player.Hp())
}

func main() {
	body := RequestBody()	
	req, err := http.NewRequest("POST", "http://localhost:8080/echo", body)
	if err != nil {
		fmt.Println(err)
		return
	}

	client := http.DefaultClient
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}

	ReadResponse(resp)
}