report non-200 status codes

This commit is contained in:
Tevildo 2024-04-18 23:32:04 +01:00
parent 604c8b02d0
commit 78539b35e2
No known key found for this signature in database
GPG key ID: 780413157E8C9E35

View file

@ -2,6 +2,8 @@ package dns
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
@ -17,6 +19,7 @@ var (
func SetTXTRecord(domain string, dnsName string, key string, login string, apiKey string) error {
urlPath := fmt.Sprintf("/dns/%s/addRR", domain)
requestUrl := fmt.Sprintf("%s%s", baseUrl, urlPath)
log.Printf("Request URL: %v", requestUrl)
values := url.Values{"name": {dnsName}, "type": {"TXT"}, "data": {key}}
body := values.Encode()
@ -53,5 +56,13 @@ func send(login string, apiKey string, urlPath string, body string, requestUrl s
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("HTTP error code %v: %v", resp.StatusCode, string(bytes))
}
return nil
}