Skip to content

REST API POST/PATCH request without a trailing slash causes an exception #21906

@jeremystretch

Description

@jeremystretch

NetBox Edition

NetBox Community

NetBox Version

v4.5.7

Python Version

3.12

Steps to Reproduce

Make a valid, authenticated PATCH request to e.g. /api/dcim/devices/1 (no trailing slash on the URL):

curl -v -X PATCH \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json; indent=4" \
http://netbox/api/dcim/devices/1

Expected Behavior

I'm actually not sure. We can't automatically redirect (as noted in the error message below) without losing the request data. A 404 (Not Found) might be the most appropriate response.

Observed Behavior

A RuntimeError exception is raised:

Traceback (most recent call last):
  File "/home/jstretch/.venv/netbox/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jstretch/.venv/netbox/lib/python3.12/site-packages/django/utils/deprecation.py", line 311, in __call__
    response = self.process_response(request, response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jstretch/.venv/netbox/lib/python3.12/site-packages/django/middleware/common.py", line 109, in process_response
    self.get_full_path_with_slash(request)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jstretch/.venv/netbox/lib/python3.12/site-packages/django/middleware/common.py", line 87, in get_full_path_with_slash
    raise RuntimeError(
    ^

Exception Type: RuntimeError at /api/dcim/devices/1
Exception Value: You called this URL via PATCH, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining PATCH data. Change your form to point to netbox:8000/api/dcim/devices/1/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    netboxstatus: needs triageThis issue is awaiting triage by a maintainertype: bugA confirmed report of unexpected behavior in the application

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions