golang api调用失败

时间:2017-02-17 17:59:11

标签: api go certificate

我正在尝试使用证书调用restful API。我的pem文件格式为:

-----BEGIN RSA PRIVATE KEY-----
    data is here
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
    data is here
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
    data is here
-----END CERTIFICATE-----

我正在使用的代码是

//creating the certificate for call
caCert, err := ioutil.ReadFile("certFil.pem")
if err != nil {
       log.Fatal(err)
}


caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

client := &http.Client{
       Transport: &http.Transport{
              TLSClientConfig: &tls.Config{
                     RootCAs:      caCertPool,
              },
       },
}

//end of creating the certificate for call

//creating the body for call
url := "https://example.com:11215/myuri/path1/path2"
fmt.Println("URL:>", url)

var jsonStr = []byte(`{
  "My JSON" 
} `)

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Accept", "application/json;v=3")
req.Header.Set("Cache-Control", "no-cache, no-store")
req.Header.Set("Connection", "Keep-Alive")
req.Header.Set("User-Agent", "HttpClient")
req.Header.Set("Content-Type", "application/json;v=3")

//client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
       panic(err)
}
defer resp.Body.Close()

当我运行此代码时,我得到一个例外:

x509:证书对fake.com有效,而不是example.com

你能帮我解决这个问题吗?

0 个答案:

没有答案