diff --git a/dnsupdate.py b/dnsupdate.py index b5b76e9..7af2252 100755 --- a/dnsupdate.py +++ b/dnsupdate.py @@ -3,7 +3,7 @@ """A script which sends a Dynamic DNS update to the Ionos API.""" from pathlib import Path -import requests +import json, requests from yaml import load try: from yaml import CLoader as Loader @@ -70,15 +70,22 @@ def main(): apikey = f"{apikey_prefix}.{apikey_secret}" domains = config["domains"] description = config["description"] if "description" in config else "Dynamic DNS update." + payload = json.dumps( + { + "domains": domains, + "description": description, + }, + indent=2, + ) # Make an API call to retrieve the DNS update URL response = requests.post( _API_ENDPOINT_URL, - headers={"X-API-Key": apikey}, - data={ - "domains": domains, - "description": description, - } + headers={ + "Content-Type": "application/json", + "X-API-Key": apikey, + }, + data=payload ) if not response.status_code == 200: @@ -90,8 +97,12 @@ def main(): f"API request failed and returned: {error_message}" ) - update_url = response.json()["updateUrl"] - print(update_url) # DEBUG + try: + update_url = response.json()["updateUrl"] + except requests.exceptions.JSONDecodeError as e: + raise RuntimeError( + "Failed to parse update URL from API response." + ) from e # Send a dynamic DNS update using the retrieved update URL response = requests.get(update_url)