github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/greeter/cli/api/api.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  
     9  	"github.com/golang/protobuf/proto"
    10  	api "github.com/micro/micro/v2/api/proto"
    11  )
    12  
    13  func main() {
    14  
    15  	req, err := proto.Marshal(&api.Request{Get: map[string]*api.Pair{"name": {Key: "name", Values: []string{"John"}}}})
    16  	if err != nil {
    17  		fmt.Println(err)
    18  		return
    19  	}
    20  
    21  	r, err := http.Post("http://localhost:8080/greeter/say/hello", "application/protobuf", bytes.NewReader(req))
    22  	if err != nil {
    23  		fmt.Println(err)
    24  		return
    25  	}
    26  	defer r.Body.Close()
    27  
    28  	b, err := ioutil.ReadAll(r.Body)
    29  	if err != nil {
    30  		fmt.Println(err)
    31  		return
    32  	}
    33  
    34  	rsp := &api.Response{}
    35  	if err := proto.Unmarshal(b, rsp); err != nil {
    36  		fmt.Println(err)
    37  		return
    38  	}
    39  	fmt.Println(rsp.Body)
    40  }