Created
September 15, 2016 22:48
-
-
Save sysradium/db492b1d3eb8ee4d7f0ff357eb2905a2 to your computer and use it in GitHub Desktop.
Send HTTP request with X509 auth golang
This file contains hidden or 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 ( | |
"crypto/tls" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatal("Please specify filename") | |
return | |
} | |
// Load client cert | |
cert, err := tls.LoadX509KeyPair("main.cert", "main.key") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Setup HTTPS client | |
tlsConfig := &tls.Config{ | |
Certificates: []tls.Certificate{cert}, | |
} | |
tlsConfig.BuildNameToCertificate() | |
transport := &http.Transport{TLSClientConfig: tlsConfig} | |
client := &http.Client{Transport: transport} | |
fileName := os.Args[1] | |
f, err := os.Open(fileName) | |
if err != nil { | |
log.Fatalln(err) | |
return | |
} | |
req, err := http.NewRequest("POST", "https://some_domain.com/xml", f) | |
req.Header.Add("Content-Type", "text/xml;charset=UTF-8") | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
_, err = io.Copy(os.Stdout, resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment