Last active
January 9, 2018 19:54
-
-
Save dnldd/fe490d393314ebd6219c3ff076f86439 to your computer and use it in GitHub Desktop.
json 2.0 test helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/tls" | |
"crypto/x509" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"path/filepath" | |
"github.com/decred/dcrd/dcrutil" | |
) | |
func main() { | |
const ( | |
url = "https://127.0.0.1:19109" | |
rpcUser = "" | |
rpcPass = "" | |
) | |
// add json requests | |
reqs := []string{} | |
// Connect to local dcrd RPC server using websockets. | |
dcrdHomeDir := dcrutil.AppDataDir("dcrd", false) | |
certs, err := ioutil.ReadFile(filepath.Join(dcrdHomeDir, "rpc.cert")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pool := x509.NewCertPool() | |
pool.AppendCertsFromPEM(certs) | |
client := http.Client{ | |
Transport: &http.Transport{ | |
TLSClientConfig: &tls.Config{ | |
RootCAs: pool, | |
}, | |
}, | |
} | |
for _, req := range reqs{ | |
bodyReader := bytes.NewReader([]byte(req)) | |
httpReq, err := http.NewRequest("POST", url, bodyReader) | |
if err != nil { | |
log.Fatal(err) | |
} | |
httpReq.Close = true | |
httpReq.Header.Set("Content-Type", "application/json") | |
httpReq.SetBasicAuth(rpcUser, rpcPass) | |
resp, err := client.Do(httpReq) | |
if err != nil { | |
log.Fatal(err) | |
} | |
respBytes, err := ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(respBytes)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment