06-07-2020 04:00 AM
I have a fully working python script that can POST objects to the FMC (6.5.0.4) API using a .csv file for the data.
I want to use this same approach/method to DELETE objects, but after many days I have been unable to get the script to work.
Initially I was adding the object to the script as data rather than in the URL, I fixed this using a single object, if I add the object into the script using:
del_data = ['005056A6-6972-0ed3-0000-090194313992']
the URL works and the objects deletes.
But this defeats the purpose of the script as I can delete a single object in simpler ways.
I believe I now have a script that adds the host object ID to the url but it continues to fail with a url 404 error:
Connection Error --> 404 Client Error: Not Found for url: https://<FMC-NAME>/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314504
I'm new to this Python scripting business and I would be grateful if anyone has any pointers.
The relevant parts to my script are below:
Thanks in advance
URL build script component
f = open("delid.csv")
objectsfile = csv.DictReader(f)
for object in objectsfile:
del_data = {
object["objectid"]
}
for object in del_data:
del_url = server + api_path + object
try:
r = requests.delete(del_url, headers=headers, verify=False)
status_code = r.status_code
resp = r.text
log = open('delete_objects.log', 'a')
print(" Status Code: "+str(status_code))
json_resp = json.loads(resp)
log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
if status_code == 201 or status_code == 202:
print(" SUCCESS ")
elif status_code == 400:
print((" Message: ")+ resp + ('\n'))
else:
r.raise_for_status()
print((" Message: ")+ resp + ('\n'))
except requests.exceptions.HTTPError as err:
print("Connection Error --> "+str(err))
finally:
if r: r.close()
Solved! Go to Solution.
06-07-2020 09:56 AM
Weird, my reply got discarded I think?
I'll repost it:
f = open("del-hostid.csv") elementsfile = csv.DictReader(f) for element in elementsfile: del_data = { element["objectid"], } for element in del_data: del_url = server + api_path + element try: print(del_url) r = requests.delete(del_url, headers=headers, verify=False) status_code = r.status_code resp = r.text log = open('delete_objects.log', 'a') print(" Status code: "+str(status_code)) json_resp = r.json() log.write('\n=====\n') log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': '))) if status_code == 201 or status_code == 202: print(" SUCCESS ") elif status_code == 400 or status_code == 404: print(" Message: " + resp + '\n') else: r.raise_for_status() print(" Message: " + resp + '\n') except requests.exceptions.HTTPError as err: print("Connection error -> "+str(err))
You had a double for-loop which wasn't needed.
# Old : elif status_code == 400: # Old : print((" Message: ")+ resp + ('\n'))
# New : elif status_code == 400 or status_code == 404:
# New : print(" Message: " + resp + '\n')
I've changed these two things:
# Old : json_resp = json.loads(resp)
# New : json_resp = r.json()
06-07-2020 12:20 PM
06-07-2020 12:38 PM
First time really replying in here, might've been me messing up something.
So you do this:
resp = r.text
json_resp = json.loads(resp)
What this does is that it converts the JSON into a Python dictionary. How ever, the response object already has this built in. In other words, you can change those two lines into this
json_resp = r.json()
This works since `.json()` is a function that takes the response.text and converts it into a Python dictionary. So it’s doing the exact same thing, it’s just often easier to do, so I wanted to show you :) sorry if it confused you.
06-07-2020 01:21 PM
06-07-2020 10:33 PM
You’re welcome. :) I appreciate the helpful votes!
Good luck!
06-08-2020 01:39 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide