API post with Python

912 views
Skip to first unread message

Craig

unread,
Aug 17, 2017, 1:27:01 PM8/17/17
to NetBox
I'm trying the following via a python script, but it is not working for me. Is netbox designed to accept posts to the API with anything other than curl? Am I missing something? I've also tried using data=data instead of json=data


import requests

url
= 'https://<hostname>/api/dcim/sites'
data
= {"name" : "my new site" , "slug" : "my-new-site"}
headers
= {'X-CSRFToken': 'f8581ac22fca6067a3f15b8a3227138f08e8', 'Content-Type': 'application/json', 'Accept': 'application/json'}
response
= requests.post(url, json=data, headers=headers, verify=False)


I've also tried it this way:

import requests

url
= 'https://netbox-sf-1.mdtmi.org/api/dcim/sites'
data
= {"name" : "my new site" , "slug" : "my-new-site"}
headers
= {'Authorization': 'Token f8581ac22fca6067a3f15b8a3227138f08e8', 'Content-Type': 'application/json', 'Accept': 'application/json'}
response
= requests.post(url, json=data, headers=headers, verify=False)



Patrick Fish

unread,
Aug 17, 2017, 3:08:10 PM8/17/17
to Craig, NetBox
Here's a small snippet that I use...whats your response.status_code after the request?

apiurl = "http://netbox/api/dcim/sites/%s/" % siteid
payload = "{ \"name\":\"%s\", \"slug\":\"%s\", \"asn\":\"%s\" }" % (store, store, bgp_as)
r2 = requests.put(apiurl, headers={"Authorization": "Token aaaadddd", "Content-Type": "application/json"}, data=payload)

print r2.status_code

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/4936b11a-d4b2-4f9b-8c04-5ac2f8c88a5f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Craig

unread,
Aug 17, 2017, 3:47:36 PM8/17/17
to NetBox
Oh maybe i could try putting the entire payload in quotes... I'll give that a shot. I'm getting return code 200. What do you get when the post is successful?

Craig

unread,
Aug 17, 2017, 3:52:05 PM8/17/17
to NetBox
I tried with quotes around the whole payload and that didn't fix it either. I wonder if it has something to do with the verify=False. I unfortunately can't connect without it because there's not cert for the site.

Craig

unread,
Aug 18, 2017, 10:50:05 AM8/18/17
to NetBox
I'm thinking more and more this might something with not liking that there's no valid cert chain. Can you try your snippet that works already, and add in verify=False to see what the result is? If that breaks it, then I can almost guarantee that's my problem.


On Thursday, August 17, 2017 at 3:08:10 PM UTC-4, Patrick Fish wrote:
Here's a small snippet that I use...whats your response.status_code after the request?

apiurl = "http://netbox/api/dcim/sites/%s/" % siteid
payload = "{ \"name\":\"%s\", \"slug\":\"%s\", \"asn\":\"%s\" }" % (store, store, bgp_as)
r2 = requests.put(apiurl, headers={"Authorization": "Token aaaadddd", "Content-Type": "application/json"}, data=payload)

print r2.status_code
On Thu, Aug 17, 2017 at 10:27 AM, Craig <craig.sl...@gmail.com> wrote:
I'm trying the following via a python script, but it is not working for me. Is netbox designed to accept posts to the API with anything other than curl? Am I missing something? I've also tried using data=data instead of json=data


import requests

url
= 'https://<hostname>/api/dcim/sites'
data
= {"name" : "my new site" , "slug" : "my-new-site"}
headers
= {'X-CSRFToken': 'f8581ac22fca6067a3f15b8a3227138f08e8', 'Content-Type': 'application/json', 'Accept': 'application/json'}
response
= requests.post(url, json=data, headers=headers, verify=False)


I've also tried it this way:

import requests

url
= 'https://netbox-sf-1.mdtmi.org/api/dcim/sites'
data
= {"name" : "my new site" , "slug" : "my-new-site"}
headers
= {'Authorization': 'Token f8581ac22fca6067a3f15b8a3227138f08e8', 'Content-Type': 'application/json', 'Accept': 'application/json'}
response
= requests.post(url, json=data, headers=headers, verify=False)



--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.

Craig

unread,
Aug 21, 2017, 12:33:55 PM8/21/17
to NetBox
Hey Patrick,

Can you try your snippet that works already, and add in verify=False as an additional argument to see if that breaks it\? It would be greatly appreciated.

Thanks,
-Craig


On Thursday, August 17, 2017 at 3:08:10 PM UTC-4, Patrick Fish wrote:
Here's a small snippet that I use...whats your response.status_code after the request?

apiurl = "http://netbox/api/dcim/sites/%s/" % siteid
payload = "{ \"name\":\"%s\", \"slug\":\"%s\", \"asn\":\"%s\" }" % (store, store, bgp_as)
r2 = requests.put(apiurl, headers={"Authorization": "Token aaaadddd", "Content-Type": "application/json"}, data=payload)

print r2.status_code
On Thu, Aug 17, 2017 at 10:27 AM, Craig <craig.sl...@gmail.com> wrote:
I'm trying the following via a python script, but it is not working for me. Is netbox designed to accept posts to the API with anything other than curl? Am I missing something? I've also tried using data=data instead of json=data


import requests

url
= 'https://<hostname>/api/dcim/sites'
data
= {"name" : "my new site" , "slug" : "my-new-site"}
headers
= {'X-CSRFToken': 'f8581ac22fca6067a3f15b8a3227138f08e8', 'Content-Type': 'application/json', 'Accept': 'application/json'}
response
= requests.post(url, json=data, headers=headers, verify=False)


I've also tried it this way:

import requests

url
= 'https://netbox-sf-1.mdtmi.org/api/dcim/sites'
data
= {"name" : "my new site" , "slug" : "my-new-site"}
headers
= {'Authorization': 'Token f8581ac22fca6067a3f15b8a3227138f08e8', 'Content-Type': 'application/json', 'Accept': 'application/json'}
response
= requests.post(url, json=data, headers=headers, verify=False)



--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages