Skip to content

Instantly share code, notes, and snippets.

@tasleson
Created April 24, 2017 19:42
Show Gist options
  • Save tasleson/4d75bf47c0018569f8a4ad124d961b0b to your computer and use it in GitHub Desktop.
Save tasleson/4d75bf47c0018569f8a4ad124d961b0b to your computer and use it in GitHub Desktop.
Example go code to talk to targetd
package main
import "fmt"
import "net/http"
import "log"
import "encoding/json"
import "bytes"
import "io/ioutil"
func main() {
type Json_Rpc struct {
Id int64 `json:"id"`
Method string `json:"method"`
Params map[string]interface{} `json:"params"`
Jsonrpc string `json:"jsonrpc"`
}
params := map[string]interface{}{ "pool":"vg-targetd/thin_pool" }
m := Json_Rpc{0, "vol_list", params, "2.0" }
b, err := json.Marshal(m)
if err != nil {
log.Fatal(err)
}
fmt.Println("Request length = ", len(b))
fmt.Println("Request payload", string(b))
var client = &http.Client{}
req, err := http.NewRequest(
"POST", "http://localhost:18700/targetrpc", bytes.NewBuffer(b))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth("admin", "fubar")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment