func main() {
	// Create new request to service go.micro.service.go-template, method Example.Call
	req := client.NewRequest("go.micro.service.template", "Example.Call", &example.Request{
		Name: proto.String("John"),
	})

	// Set arbitrary headers
	req.Headers().Set("X-User-Id", "john")
	req.Headers().Set("X-From-Id", "script")

	rsp := &example.Response{}

	// Call service
	if err := client.Call(req, rsp); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(rsp.GetMsg())
}
Exemple #2
0
func main() {
	// Create new request to service go.micro.service.go-template, method Example.Call
	req := client.NewRequest("sample.math", "Math.Square", &math.Request{
		Num: proto.Int(4),
	})

	// Set arbitrary headers
	req.Headers().Set("X-User-Id", "john")
	req.Headers().Set("X-From-Id", "script")

	rsp := &math.Response{}

	// Call service
	if err := client.Call(req, rsp); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(rsp.GetNum())

	// now call it using JSON RPC
	var n int32
	n = 4

	// note using standard types here, but the input protocol
	// requires a pointer to an int instead of an int
	// so we dance a little first (above)
	req1 := client.NewJsonRequest("sample.math", "Math.Square", &math.Request{
		Num: &n,
	})
	rsp = &math.Response{}
	// Call service
	if err := client.Call(req1, rsp); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(rsp.GetNum())

}
func (c *serverContext) NewRequest(service, method string, request interface{}) client.Request {
	req := client.NewRequest(service, method, request)
	// TODO: set headers and scope
	req.Headers().Set("X-User-Session", c.Request().Session("X-User-Session"))
	return req
}