Using API via Python Using API via Python

Topic: Using API via Python

Post Using API via Python
by vier on Thursday, June 13, 2024

Bash works but I cannot seem to get it to work via python. The debug output from debug is below after the python source code. Have I made any obvious errors?

#!/usr/bin/env bash

VAL=`curl -X POST https://api.dynu.com/v2/dns/{ID}/record/{RID} -H "accept: application/json" -H "API-Key: {KEY}" -d "{ \"nodeName\": \"_acme-challenge\", \"recordType\": \"TXT\", \"state\": true, \"textData\": \"DATA\" }"`
echo $VAL | python -m json.tool


Reponse:
{ "statusCode": 200, ... }

======

#!/usr/bin/env python
import requests

url = "https://api.dynu.com/v2/dns"
hostid = {ID}
recordid = {RID}
type = "record"

url = url +"/"+ str(hostid) +"/"+ type +"/"+ str(recordid);

header = {
"accept": "application/json",
"API-Key": "{KEY}"
}

pd = {
"nodeName": "_acme-challenge",
"recordType": "TXT",
"state": "true",
"textData": "Python"
}

response = requests.post(url, data=pd, headers=header)


Response:

{'accept': 'application/json', 'API-Key': '{{KEY}'}
{'nodeName': '_acme-challenge', 'recordType': 'TXT', 'state': 'true', 'textData': 'Python'}
https://api.dynu.com/v2/dns/{ID}/record/{RID}

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.dynu.com:443
send: b'POST /v2/dns/{ID}/record/{RID} HTTP/1.1\r\nHost: api.dynu.com\r\nUser-Agent: python-requests/2.32.1\r\nAccept-Encoding: gzip, deflate\r\naccept: application/json\r\nConnection: keep-alive\r\nAPI-Key: {KEY}\r\nContent-Length: 66\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'nodeName=_acme-challenge&recordType=TXT&state=true&textData=Python'
reply: 'HTTP/1.1 501 Argument Exception\r\n'
header: Date: Thu, 13 Jun 2024 18:58:25 GMT
header: Server: Dynu Web Server
header: X-Powered-By: Dynu Dynamic DNS Service
header: Cache-Control: no-store
header: Pragma: no-cache
header: Content-Length: 67
header: Content-Type: application/json; charset=UTF-8
DEBUG:urllib3.connectionpool:https://api.dynu.com:443 "POST /v2/dns/{ID}/record/{RID} HTTP/1.1" 501 67

Reply with quote | Report
Sunday, December 22, 2024 2:13 AM
Loading...