// JSONRPCRequest calls the given method via JSON-RPC over HTTPS. // It supplies the given JSON args to the called method. func (c *URLClient) JSONRPCRequest(method string, args interface{}) (map[string]interface{}, error) { if args == nil { // a nil argument would trigger an error, send empty object instead args = struct{}{} } buf, err := json2.EncodeClientRequest(method, args) if err != nil { return nil, err } // create new client with activated TLS client := &http.Client{Transport: c.transport} body := bytes.NewBuffer(buf) // make HTTP request request, err := http.NewRequest("POST", c.curl, body) if err != nil { return nil, err } defer request.Body.Close() request.Header.Set("Content-Type", "application/json") resp, err := client.Do(request) if err != nil { return nil, err } reply := make(map[string]interface{}) err = json2.DecodeClientResponse(resp.Body, &reply) if err != nil { return nil, err } resp.Body.Close() return reply, nil }
func call(name, url string, args map[string]interface{}) (map[string]interface{}, error) { msg, err := json2.EncodeClientRequest(name, []interface{}{args}) if err != nil { return nil, err } req, err := http.NewRequest("POST", url, bytes.NewBuffer(msg)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") //dreq, _ := httputil.DumpRequest(req, true) //log.Println(string(dreq)) client := new(http.Client) resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() //dresp, _ := httputil.DumpResponse(resp, true) //log.Println(string(dresp)) reply := make(map[string]interface{}) err = json2.DecodeClientResponse(resp.Body, &reply) if err != nil { return nil, err } return reply, nil }
func (p Client) Call(method string, args interface{}, result interface{}) error { message, err := json2.EncodeClientRequest(method, args) if err != nil { log.Printf("%s", err) return err } req, err := http.NewRequest("POST", p.Url, bytes.NewBuffer(message)) if err != nil { log.Printf("%s", err) return err } req.Header.Set("Content-Type", "application/json") client := new(http.Client) resp, err := client.Do(req) if err != nil { log.Printf("Error in sending request to %s. %s", p.Url, err) return err } defer resp.Body.Close() err = json2.DecodeClientResponse(resp.Body, result) if err != nil { log.Printf("Couldn't decode response. %s", err) return err } return err }
func main() { if len(os.Args) < 2 { fmt.Fprintf(os.Stderr, "not enough arguments\n") os.Exit(1) } serverStr := os.Args[1] params := make(map[string]string) params["cmdTxt"] = strings.Join(os.Args[2:], " ") params["cmdOrigin"] = "terminal" buf, err := json2.EncodeClientRequest("RombaService.Execute", params) if err != nil { fmt.Fprintf(os.Stderr, "failed to encode json2 client request: %v\n", err) os.Exit(1) } body := bytes.NewBuffer(buf) resp, err := http.Post("http://"+serverStr+"/jsonrpc/", "application/json", body) if err != nil { fmt.Fprintf(os.Stderr, "failed to issue client request: %v\n", err) os.Exit(1) } reply := new(Reply) err = json2.DecodeClientResponse(resp.Body, reply) if err != nil { fmt.Fprintf(os.Stderr, "failed to decode response: %v\n", err) os.Exit(1) } fmt.Printf("%s\n", reply.Message) }
func Ping(a int) { // Where are we going to send this puppy? url := "http://servicebus/Execute.svc/Execute" // Procedure Arguments Struct type Args struct { Delaymilliseconds int } // Procedure Arguments Struct type Result struct { PingResponse string } // Procedure Arguments Filled out args := &Args{ Delaymilliseconds: a, } // Encode the message message, err := json2.EncodeClientRequest("Utility.Ping", args) if err != nil { log.Fatalf("%s", err) } log.Printf("Sending Request: %s\n", message) // Create the request req, err := http.NewRequest("POST", url, bytes.NewBuffer(message)) if err != nil { log.Fatalf("%s", err) } // Set the content type req.Header.Set("Content-Type", "application/json") // Make a client client := new(http.Client) // Do the post resp, err := client.Do(req) if err != nil { log.Fatalf("Error in sending request to %s. %s", url, err) } defer resp.Body.Close() bomstripper := &bomstripper.BomStrip{R: resp.Body} var result Result // err = json2.DecodeClientResponse(resp.Body, &result) err = json2.DecodeClientResponse(bomstripper, &result) if err != nil { log.Fatalf("Couldn't decode response. %s", err) } log.Printf("Result was %s\n", result) }
// Call invokes the named function, waits for it to complete, and returns its error status. func (c Client) Call(serviceMethod string, args interface{}, reply interface{}) error { buf, err := json2.EncodeClientRequest(serviceMethod, args) if err != nil { return err } req, err := http.NewRequest("POST", c.addr, bytes.NewBuffer(buf)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { return err } return json2.DecodeClientResponse(resp.Body, reply) }
// Calls method of JSONRPC func (service *JsonRpcService) CallMethod(method string, params interface{}, reply interface{}) (*JsonRpcHttpInfo, error) { jsonrpcRequest, jsonrpcErr := json2.EncodeClientRequest(method, params) if jsonrpcErr != nil { return nil, fmt.Errorf("Encode request of JSONRPC(2.0) has error: %v", jsonrpcErr) } /** * Calls the method to service */ status, headers, body, err := service.httpClient.Post( service.url, map[string][]string{"Content-Type": []string{"application/json"}}, strings.NewReader(string(jsonrpcRequest)), ) if body != nil { defer body.Close() } /** * Binds the information of HTTP response */ httpInfo := JsonRpcHttpInfo{ Status: status, Headers: headers, } // :~) if err != nil { return &httpInfo, fmt.Errorf("Call method[%v, %v] has error: %v", method, service.url, err) } // :~) /** * Unmarshal the response of JSONRPC */ err = json2.DecodeClientResponse(body, reply) if err != nil { return &httpInfo, fmt.Errorf("Decode response has error: %v. Method[%v, %v]", err, method, service.url) } // :~) return &httpInfo, nil }
func main() { url := "http://192.168.33.10:8000/index.php" var params [1]string params[0] = os.Args[1] message, err := json2.EncodeClientRequest("parse", params) 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 Result err = json2.DecodeClientResponse(resp.Body, &result) if err != nil { log.Fatalf("Couldn't decode response. %s", err) } fmt.Printf("Input number: %s\n", params[0]) fmt.Printf("Number valid: %t\n", result.Valid) if result.Valid == true { fmt.Printf("Country dialing code: %d\n", result.CountryDiallingCode) fmt.Printf("Country identifier: %s\n", result.CountryIdentifier) fmt.Printf("MNO identifier: %s\n", result.MnoIdentifier) fmt.Printf("Subscriber number: %s\n", result.SubscriberNumber) } }