I am using the Requests python module to do RESTFUL POST API calls to create a new item.
PROBLEM: Items are being duplicated. Sometimes 3 of each item get created. No check for existing data is in place in the final api prevent duplication.
GOAL: I want to send data to create a single new item in a third party application called Netbox which is made with Django.
WHAT HAVE I DONE?:
- I have tried writing code to check for existing items and it still makes duplicates.
- I have tried set a timer to pause execution of code by 10 seconds until the request is complete.
The process flow looks like this:
Python Backend (Flask) ---> Python Backend (Flask) --> Netbox (Django)
Data Origin sent via Requests ---> Data Netbox Calls (Via PyNetBox) --> Final DataStore
The two backends are hosted in Cloud Foundry while Netbox is spun up on a Linux on Prem Server.
I have a dictionary like below that represents each item I need to create in Netbox.
Ex. payload = {'site':'1', 'device':'switch01'}
I send each piece of data to my middleware backend via a loop to make the final api call to netbox via pynetbox. I am not able to hit netbox directly.
for payload in payloads:
URL = f'{MIDDLEWARE_URL}/netbox/create_item'
print(f'URL is: {URL} | Payload is {payload}')
headers = { 'content-type': 'application/json', 'cache-control': "no-cache"}
proxies={'http' : None, 'https': None}
r = requests.post(URL, data=json.dumps(payload), headers=headers, verify=False, proxies=proxies)
I would appreciate any help as to why this is happening and any best practices to improving my baseline code logic.
All the best, Faraz
data=json.dumps(payload)you can usejson=payloadand it should also add'content-type': 'application/json'automatically.