func BuyStocks(method string, args ReqParameters) (Response ResParameters, err error) { buf, _ := json.EncodeClientRequest(method, args) fmt.Println("Symbols in jsonrpccall", args.Symbols) //fmt.Println("buffer : ", bytes.NewBuffer(buf)) req, err := http.NewRequest("POST", "http://localhost:1234/rpc", bytes.NewBuffer(buf)) req.Header.Set("X-Custom-Header", "myvalue") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return } /* defer resp.Body.Close() fmt.Println("response Status:", resp.Status) fmt.Println("response Headers:", resp.Header) body, _ := ioutil.ReadAll(resp.Body) fmt.Println("response Body:", string(body)) */ err = json.DecodeClientResponse(resp.Body, &Response) /*if err != nil { return }*/ //var store = sessions.NewCookieStore([]byte("something-very-secret")) /* cookies := resp.Cookies() //cookie, _ := req.Cookie("golang-cookie") fmt.Println("cookies", cookies) fmt.Println("Session name", Session.Name()) */ return }
func main() { url := "http://localhost:1234/rpc" args := &rpcexample.Args{ A: 2, B: 3, } message, err := json.EncodeClientRequest("Arith.Multiply", args) if err != nil { log.Fatalf("%s", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(message)) if err != nil { log.Fatalf("%s", err) } req.Header.Set("Content-Type", "application/json") client := new(http.Client) resp, err := client.Do(req) if err != nil { log.Fatalf("Error in sending request to %s. %s", url, err) } defer resp.Body.Close() var result rpcexample.Result err = json.DecodeClientResponse(resp.Body, &result) if err != nil { log.Fatalf("Couldn't decode response. %s", err) } log.Printf("%d*%d=%d\n", args.A, args.B, result) }
func (self *Client) Call(service string, args interface{}, reply interface{}) error { message, err := json.EncodeClientRequest(service, args) //log.Println(string(message)) if err != nil { return err } req, err := http.NewRequest("POST", self.addr, bytes.NewBuffer(message)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") resp, err := self.client.Do(req) if err != nil { return err } /*var bodyBytes []byte if resp.Body != nil { bodyBytes, _ = ioutil.ReadAll(resp.Body) } // Restore the io.ReadCloser to its original state resp.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // Use the content bodyString := string(bodyBytes) log.Println(bodyString)*/ defer resp.Body.Close() err = json.DecodeClientResponse(resp.Body, reply) if err != nil { log.Println("Couldn't decode response. %s", err) return err } return nil }
func main() { flag.Parse() if len(os.Args) < 2 { log.Fatal("Not enough args") } body, err := json.EncodeClientRequest("Rexd.Execute", &service.ExecuteArgs{ Token: "nyraITVmr61ALZNdf9Ye", Name: "deploy_api", Branch: "master", Args: nil, }) if err != nil { log.Fatal(err) } resp, err := http.Post(*apiURL, "application/json", bytes.NewReader(body)) if err != nil { log.Fatal(err) } rb, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } log.Print(string(rb)) }
func (c *Client) call(httpMethod string, rpcMethod string, args interface{}, result interface{}) (err error) { var ( method string = fmt.Sprintf("%v.%v", SERVICE_NAME, rpcMethod) message []byte req *http.Request resp *http.Response ) if message, err = json.EncodeClientRequest(method, args); err != nil { return } msgBuf := bytes.NewBuffer(message) if req, err = http.NewRequest(httpMethod, c.endpoint, msgBuf); err != nil { return } req.Header.Set("Content-Type", "application/json") if resp, err = c.httpClient.Do(req); err != nil { return } defer resp.Body.Close() if err = json.DecodeClientResponse(resp.Body, result); err != nil { return } return }
func jsonRpcTradeCall(method string, args RPCAPITRADEArguement) (reply RPCAPITradeResponse, err error) { buf, _ := json.EncodeClientRequest(method, args) resp, err := http.Post("http://localhost:8080/rpc", "application/json", bytes.NewBuffer(buf)) if err != nil { return } defer resp.Body.Close() err = json.DecodeClientResponse(resp.Body, &reply) return }
func jsonrpc(method string, req, res interface{}) error { buf, err := json.EncodeClientRequest(method, req) if err != nil { return err } resp, err := http.Post(fmt.Sprintf("http://localhost:%d/rpc", env.Port), "application/json", bytes.NewBuffer(buf)) if err != nil { return err } defer resp.Body.Close() json.DecodeClientResponse(resp.Body, res) return nil }
func ClientRequest(method string, args Args) (reply Reply, err error) { req, err := json.EncodeClientRequest(method, args) if err != nil { fmt.Println("ClientRequest Error") fmt.Println(err) return reply, err } res, err := http.Post("http://127.0.0.1:8080/rpc", "application/json", bytes.NewBuffer(req)) if err != nil { fmt.Println("Post Error") fmt.Println(err) return reply, err } defer res.Body.Close() err = json.DecodeClientResponse(res.Body, &reply) return reply, err }
func RPC(method string, args InputArgs) (reply ReplyMessage, err error) { buf, err := json.EncodeClientRequest(method, args) if err != nil { return reply, err } resp, err := http.Post("http://127.0.0.1:8080/rpc", "application/json", bytes.NewBuffer(buf)) if err != nil { fmt.Println("Http.Post Error") return reply, err } defer resp.Body.Close() err = json.DecodeClientResponse(resp.Body, &reply) return reply, err }
func RPCCall(method string, req *ButlerOrder, res *ButlerServe) error { buf, _ := json.EncodeClientRequest(method, req) body := bytes.NewBuffer(buf) httpReq, err := http.NewRequest("POST", "http://localhost:10080/rpc", body) httpReq.Header.Set("Content-Type", "application/json") client := &http.Client{} r, err := client.Do(httpReq) if err != nil { return err } defer r.Body.Close() err = json.DecodeClientResponse(r.Body, res) if err != nil { return err } return nil }
func main() { var option int var money float32 var input string var errChar string bio := bufio.NewReader(os.Stdin) fmt.Println("Choose one of the following options: ") fmt.Println("1. Buy stocks") fmt.Println("2. View profile") fmt.Println("Enter 1 or 2") line, _, _ := bio.ReadLine() fmt.Sscanf(string(line), "%d %s", &option, &errChar) if errChar != "" { fmt.Println("Invalid input") os.Exit(1) } if option == 1 { //Get input var a Request1 var reply Reply fmt.Println("Enter string input") line, _, _ := bio.ReadLine() fmt.Sscanf(string(line), "%s", &input) fmt.Println("Enter budget") line2, _, _ := bio.ReadLine() fmt.Sscanf(string(line2), "%f", &money) a.Budget = money a.StockSymbolAndPercentage = input buf, _ := json.EncodeClientRequest("StockMarket.BuyStocks", &a) resp, err := http.Post("http://localhost:10000/rpc", "application/json", bytes.NewBuffer(buf)) if err != nil { fmt.Println("Error occurred", err) } defer resp.Body.Close() err = json.DecodeClientResponse(resp.Body, &reply) if err != nil { log.Fatal("Error occurred.. ", err) } fmt.Println("Your trade Id is: ", reply.TradeId) fmt.Println("Summary of your trade: ", reply.StrOut) fmt.Println("Remaining balance: ", reply.UnvestedAmount) } else if option == 2 { var tid int var b Request2 var reply2 Response2 fmt.Println("Enter your trade id") line, _, _ := bio.ReadLine() fmt.Sscanf(string(line), "%d %s", &tid, &errChar) if errChar != "" { fmt.Println("Invalid input") os.Exit(1) } b.TradeId = tid buf, _ := json.EncodeClientRequest("StockMarket.CheckPortfolio", &b) resp2, err := http.Post("http://localhost:10000/rpc", "application/json", bytes.NewBuffer(buf)) if err != nil { fmt.Println("Error:", err) } defer resp2.Body.Close() err = json.DecodeClientResponse(resp2.Body, &reply2) if err != nil { log.Fatal(err) } fmt.Println("Stocks bought: ", reply2.Stocks) fmt.Println("CurrentMarketValue: ", reply2.CurrentMarketValue) fmt.Println("Remaining balance: ", reply2.UnvestedAmount) } else { fmt.Println("Invalid input") } }
func (jsonCodec) EncodeRequest(method string, args interface{}) ([]byte, error) { return json.EncodeClientRequest(method, args) }