fix: missing Content-Type-header in API request

This commit is contained in:
Julian Lobbes 2022-09-03 01:26:06 +02:00
parent 4f1547a61b
commit 4d484ebd60

View file

@ -3,7 +3,7 @@
"""A script which sends a Dynamic DNS update to the Ionos API.""" """A script which sends a Dynamic DNS update to the Ionos API."""
from pathlib import Path from pathlib import Path
import requests import json, requests
from yaml import load from yaml import load
try: try:
from yaml import CLoader as Loader from yaml import CLoader as Loader
@ -70,15 +70,22 @@ def main():
apikey = f"{apikey_prefix}.{apikey_secret}" apikey = f"{apikey_prefix}.{apikey_secret}"
domains = config["domains"] domains = config["domains"]
description = config["description"] if "description" in config else "Dynamic DNS update." 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 # Make an API call to retrieve the DNS update URL
response = requests.post( response = requests.post(
_API_ENDPOINT_URL, _API_ENDPOINT_URL,
headers={"X-API-Key": apikey}, headers={
data={ "Content-Type": "application/json",
"domains": domains, "X-API-Key": apikey,
"description": description, },
} data=payload
) )
if not response.status_code == 200: if not response.status_code == 200:
@ -90,8 +97,12 @@ def main():
f"API request failed and returned: {error_message}" f"API request failed and returned: {error_message}"
) )
update_url = response.json()["updateUrl"] try:
print(update_url) # DEBUG 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 # Send a dynamic DNS update using the retrieved update URL
response = requests.get(update_url) response = requests.get(update_url)