OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • Development and Code Review (Moderator: fabian) »
  • API Help - Bind update record - probably missing something easy.
« previous next »
  • Print
Pages: [1]

Author Topic: API Help - Bind update record - probably missing something easy.  (Read 398 times)

5SpeedFun

  • Full Member
  • ***
  • Posts: 106
  • Karma: 7
    • View Profile
API Help - Bind update record - probably missing something easy.
« 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.
Logged

5SpeedFun

  • Full Member
  • ***
  • Posts: 106
  • Karma: 7
    • View Profile
Re: API Help - Bind update record - probably missing something easy.
« Reply #1 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"}'
Logged

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • Development and Code Review (Moderator: fabian) »
  • API Help - Bind update record - probably missing something easy.
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2023 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2