curl --cert ./clientcert.pem --cert-type PEM --key ./clientkey.pem --cert-type PEM --cacert ./cacerts.pem --from "files=@test.xml" "https://example.com"
Как преобразовать эту команду curl в golang? Мой код выглядит так:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"crypto/tls"
"crypto/x509"
"flag"
"bytes"
)
func main() {
xml_temp = "./test.xml"
key_dir = "./clientkey.pem"
cert_dir = "./clientcert.pem"
cacert_dir = "./cacerts.pem"
fmt.Println("loading ",*xml_temp)
fmt.Println("loading ",*cert_dir)
fmt.Println("loading ",*key_dir)
fmt.Println("loading ",*cacert_dir)
pool := x509.NewCertPool()
caCrt, err := ioutil.ReadFile(*cacert_dir)
if err != nil {
panic("Unable to read caCert")
}
pool.AppendCertsFromPEM(caCrt)
certFile, err := ioutil.ReadFile(*cert_dir)
if err != nil {
panic("Unable to read certPem")
}
keyFile, err := ioutil.ReadFile(*key_dir)
if err != nil {
panic("Unable to read keyPem")
}
fmt.Println("signing", *xml_temp, "...")
cert, err := tls.X509KeyPair(certFile, keyFile)
if err != nil {
panic("Unable to read privateKey")
}
cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: cfg,
},
}
url := "https://example.com"
xml_body,err := ioutil.ReadFile(*xml_temp)
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(xml_body)))
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
, почему это не работает. Я всегда получаю сообщение об ошибке
Ваш запрос не содержит действительный токен безопасности.
Может ли кто-нибудь мне помочь?