control-panel-dns/cloudflare/http.go

108 lines
2.3 KiB
Go
Raw Normal View History

2023-12-21 09:39:33 -06:00
// This is a simple example
package cloudflare
import (
"log"
"io/ioutil"
"net/http"
"bytes"
)
/*
curl --request POST \
--url https://api.cloudflare.com/client/v4/zones/zone_identifier/dns_records \
--header 'Content-Type: application/json' \
--header 'X-Auth-Email: ' \
--data '{
"content": "198.51.100.4",
"name": "example.com",
"proxied": false,
"type": "A",
"comment": "Domain verification record",
"tags": [
"owner:dns-team"
],
"ttl": 3600
}'
*/
func doCurl(method string, rr *RRT) string {
var err error
var req *http.Request
2023-12-21 09:39:33 -06:00
data := []byte(rr.data)
2023-12-21 09:39:33 -06:00
if (method == "PUT") {
req, err = http.NewRequest(http.MethodPut, rr.url, bytes.NewBuffer(data))
} else {
req, err = http.NewRequest(http.MethodPost, rr.url, bytes.NewBuffer(data))
}
2023-12-21 09:39:33 -06:00
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Key", rr.Auth)
req.Header.Set("X-Auth-Email", rr.Email)
log.Println("http PUT url =", rr.url)
log.Println("http PUT Auth =", rr.Auth)
log.Println("http PUT Email =", rr.Email)
log.Println("http PUT data =", rr.data)
2023-12-21 09:39:33 -06:00
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return ""
2023-12-21 09:39:33 -06:00
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return ""
2023-12-21 09:39:33 -06:00
}
return string(body)
2023-12-21 09:39:33 -06:00
}
func curlPost(dnsRow *RRT) string {
var authKey string = dnsRow.Auth
var email string = dnsRow.Email
url := dnsRow.url
tmp := dnsRow.data
log.Println("curlPost() START")
log.Println("curlPost() authkey = ", authKey)
log.Println("curlPost() email = ", email)
log.Println("curlPost() url = ", url)
2023-12-21 09:39:33 -06:00
data := []byte(tmp)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Key", authKey)
req.Header.Set("X-Auth-Email", email)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return ""
}
// log.Println("http PUT body =", body)
// spew.Dump(body)
log.Println("result =", string(body))
log.Println("curl() END")
pretty, _ := FormatJSON(string(body))
return pretty
}