OPNsense Forum

English Forums => Development and Code Review => Topic started by: 5SpeedFun on January 29, 2023, 06:18:58 am

Title: API Help - Bind update record - probably missing something easy.
Post by: 5SpeedFun on January 29, 2023, 06:18:58 am
So I have the following code in Python.  My goal is to fetch a TXT record, only change the value of it, and then update it.  This might be a very easy fix/issue that I overlocked, as I'm new to REST & Python.

To try to keep changes to the record as minimal as possible, I'm simply getting the record by UUID, changing one value & posting it back


Code: [Select]
   #Get Record by UUID
    getRecordURL = f"https://edge01.mydomain.com/api/bind/record/getRecord/{record_to_update}"
   
    newTXTvalue = '"abcdefg1234567"'
    myonerecord = requests.get(getRecordURL, auth=(api_key, api_secret))
    myonerecord_response = json.loads(myonerecord.text)

    #Set new Value
    myonerecord_response["record"]["value"] = f"{newTXTvalue}"

    #Pass the same json back with the value changed.
    postRecordURL = f"https://edge01.mydomain.com/api/bind/record/setRecord/{record_to_update}"
    myupdaterecord = requests.request("POST", postRecordURL, auth=(api_key, api_secret), json=myonerecord_response)

If I then do a print(mypdaterecord.content) I get:

Code: [Select]
b'{"result":"failed","validations":{"record.domain":"option not in list","record.type":"option not in list"}}'

Considering I'm returning the same json back that I got, with only a single string changed I'm not sure why I'm getitng an error.
Title: Re: API Help - Bind update record - probably missing something easy.
Post by: 5SpeedFun on January 29, 2023, 02:10:10 pm
Ok, my first error was forgetting to turn the dictionary back into json.

I've updated the POST as follows:

Code: [Select]
myupdaterecord = requests.request("POST", postRecordURL, auth=(api_key, api_secret), json=json.dumps(myonerecord_response))
However:

Code: [Select]
print(myupdaterecord.content)
now gives me:

Code: [Select]
b'{"result":"failed"}'