ValueError when API Post for New VLAN with Custom Fields #1196

Closed
opened 2025-12-29 16:29:57 +01:00 by adam · 4 comments
Owner

Originally created by @mattschwen on GitHub (Aug 24, 2017).

Issue type

[ ] Feature request
[x] Bug report
[ ] Documentation

Environment

  • Python version: 2.7.5
  • NetBox version: v2.1.3

Description

When trying to add a new VLAN via API Post with Custom Fields, I get the following Error:

Traceback (most recent call last):
  File "add_vlan_netbox.py", line 64, in <module>
    main()
  File "add_vlan_netbox.py", line 60, in main
    (postVLAN(url))
  File "add_vlan_netbox.py", line 46, in postVLAN
    response = requests.post(url, headers=headers, data=payload).json()
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 894, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

When I exclude custom fields, I am able to add the VLAN just fine.

I am using the following script to add new VLANs:

# Python
# import pdb; pdb.set_trace()

import requests, json


def postVLAN(url):

	headers = {
		'Authorization': 'Token MYHIDDENTOKEN',
		'Accept': 'application/json',
		}

	payload = {
		'name': 'test', 
		'vid': '900', 
		"custom_fields": {
			"Notes": 'notes',
			"Data Classification": "restricted",
			"Compute Platform": "all",
			"Routing Device": "befw",
			"Zone": "int"
			},
		}

	response = requests.post(url, headers=headers, data=payload).json()
	print response
	return response


def main():
	'''
	Main function, calls on other functions, start of the script.
	'''
	netbox = 'netbox.corp.adobe.com'
	netboxVlans = '/api/ipam/vlans/'
	url = 'http://%s%s' % (netbox, netboxVlans)

	(postVLAN(url))


if __name__ == '__main__':
	main()
Originally created by @mattschwen on GitHub (Aug 24, 2017). <!-- Before opening a new issue, please search through the existing issues to see if your topic has already been addressed. Note that you may need to remove the "is:open" filter from the search bar to include closed issues. Check the appropriate type for your issue below by placing an x between the brackets. If none of the below apply, please raise your issue for discussion on our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please note that issues which do not fall under any of the below categories will be closed. ---> ### Issue type [ ] Feature request <!-- Requesting the implementation of a new feature --> [x] Bug report <!-- Reporting unexpected or erroneous behavior --> [ ] Documentation <!-- Proposing a modification to the documentation --> <!-- Please describe the environment in which you are running NetBox. (Be sure to verify that you are running the latest stable release of NetBox before submitting a bug report.) --> ### Environment * Python version: 2.7.5 * NetBox version: v2.1.3 <!-- BUG REPORTS must include: * A list of the steps needed to reproduce the bug * A description of the expected behavior * Any relevant error messages (screenshots may also help) FEATURE REQUESTS must include: * A detailed description of the proposed functionality * A use case for the new feature * A rough description of any necessary changes to the database schema * Any relevant third-party libraries which would be needed --> ### Description When trying to add a new VLAN via API Post with Custom Fields, I get the following Error: ``` Traceback (most recent call last): File "add_vlan_netbox.py", line 64, in <module> main() File "add_vlan_netbox.py", line 60, in main (postVLAN(url)) File "add_vlan_netbox.py", line 46, in postVLAN response = requests.post(url, headers=headers, data=payload).json() File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 894, in json return complexjson.loads(self.text, **kwargs) File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded ``` When I exclude custom fields, I am able to add the VLAN just fine. I am using the following script to add new VLANs: ```python # Python # import pdb; pdb.set_trace() import requests, json def postVLAN(url): headers = { 'Authorization': 'Token MYHIDDENTOKEN', 'Accept': 'application/json', } payload = { 'name': 'test', 'vid': '900', "custom_fields": { "Notes": 'notes', "Data Classification": "restricted", "Compute Platform": "all", "Routing Device": "befw", "Zone": "int" }, } response = requests.post(url, headers=headers, data=payload).json() print response return response def main(): ''' Main function, calls on other functions, start of the script. ''' netbox = 'netbox.corp.adobe.com' netboxVlans = '/api/ipam/vlans/' url = 'http://%s%s' % (netbox, netboxVlans) (postVLAN(url)) if __name__ == '__main__': main() ```
adam closed this issue 2025-12-29 16:29:57 +01:00
Author
Owner

@lampwins commented on GitHub (Aug 25, 2017):

@mattschwen this works just fine for me when

response = requests.post(url, headers=headers, data=payload).json()

is changed to

response = requests.post(url, headers=headers, json=payload).json()

data is for form data where netbox is expecting a json payload.

From: http://docs.python-requests.org/en/master/api/#main-interface

data -- (optional) Dictionary or list of tuples [(key, value)] (will be form-encoded), bytes, or file-like object to send in the body of the Request.
json -- (optional) json data to send in the body of the Request.

@lampwins commented on GitHub (Aug 25, 2017): @mattschwen this works just fine for me when ``` response = requests.post(url, headers=headers, data=payload).json() ``` is changed to ``` response = requests.post(url, headers=headers, json=payload).json() ``` `data` is for form data where netbox is expecting a json payload. From: http://docs.python-requests.org/en/master/api/#main-interface > data -- (optional) Dictionary or list of tuples [(key, value)] (will be form-encoded), bytes, or file-like object to send in the body of the Request. json -- (optional) json data to send in the body of the Request.
Author
Owner

@mattschwen commented on GitHub (Aug 25, 2017):

I have changed that line and appear to still have an issue.

response = requests.post(url, headers=headers, json=payload)

Here is my response in text format:

<!DOCTYPE html>
<html lang="en">

<head>
	<title>Server Error</title>
	<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-4 col-md-offset-4">
                <div class="panel panel-danger" style="margin-top: 200px">
                    <div class="panel-heading">
                        <strong>
                            <i class="fa fa-warning"></i>
                            Server Error
                        </strong>
                    </div>
                    <div class="panel-body">
                        <p>There was a problem with your request. This error has been logged and administrative staff have
                        been notified. Please return to the home page and try again.</p>
                        <p>If you are responsible for this installation, please consider
                        <a href="https://github.com/digitalocean/netbox/issues">filing a bug report</a>. Additional
                        information is provided below:</p>
<pre><strong>&lt;type &#39;exceptions.TypeError&#39;&gt;</strong><br />
&#39;custom_fields&#39; is an invalid keyword argument for this function</pre>
                        <div class="text-right">
                            <a href="/" class="btn btn-primary">Home Page</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
@mattschwen commented on GitHub (Aug 25, 2017): I have changed that line and appear to still have an issue. `response = requests.post(url, headers=headers, json=payload)` Here is my response in text format: ```html <!DOCTYPE html> <html lang="en"> <head> <title>Server Error</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css"> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-4 col-md-offset-4"> <div class="panel panel-danger" style="margin-top: 200px"> <div class="panel-heading"> <strong> <i class="fa fa-warning"></i> Server Error </strong> </div> <div class="panel-body"> <p>There was a problem with your request. This error has been logged and administrative staff have been notified. Please return to the home page and try again.</p> <p>If you are responsible for this installation, please consider <a href="https://github.com/digitalocean/netbox/issues">filing a bug report</a>. Additional information is provided below:</p> <pre><strong>&lt;type &#39;exceptions.TypeError&#39;&gt;</strong><br /> &#39;custom_fields&#39; is an invalid keyword argument for this function</pre> <div class="text-right"> <a href="/" class="btn btn-primary">Home Page</a> </div> </div> </div> </div> </div> </div> </body> </html> ```
Author
Owner

@lampwins commented on GitHub (Aug 25, 2017):

Thanks, at this point this appears to be a duplicate of #1443 albeit extended to vlans.

@lampwins commented on GitHub (Aug 25, 2017): Thanks, at this point this appears to be a duplicate of #1443 albeit extended to vlans.
Author
Owner

@jeremystretch commented on GitHub (Aug 25, 2017):

Yeah, looks like it. I'm going to close this out as #1443 has been resolved in the develop branch and will be out in v2.1.4.

@mattschwen if you still have this issue once upgraded to v2.1.4 please ask to reopen this.

@jeremystretch commented on GitHub (Aug 25, 2017): Yeah, looks like it. I'm going to close this out as #1443 has been resolved in the develop branch and will be out in v2.1.4. @mattschwen if you still have this issue once upgraded to v2.1.4 please ask to reopen this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1196