Error creating token: api/users/tokens/provision/ #8866

Closed
opened 2025-12-29 20:42:19 +01:00 by adam · 7 comments
Owner

Originally created by @Towmin84 on GitHub (Nov 22, 2023).

NetBox version

v3.6.5

Python version

3.10

Steps to Reproduce

It does not work "Non-authenticated REST API endpoint via which a user may create a Token.":

1.- POST: /api/users/tokens/provision/
Example Value Schema 1:
{ "expires": "2023-11-22T13:18:33.129Z", "write_enabled": true, "description": "string", "username": "string", "password": "string" }

 Example Value Schema 2:
 `{
      "username": "string",
      "password": "string"
    }`

Expected Behavior

A 201 response like:
{ "id": 0, "url": "string", "display": "string", "user": { "id": 0, "url": "string", "display": "string", "username": "tTnthh-HVxpS.@a@fEMI+mkEa_.pCGXhb6ZcQKWJBITD1tL8UnCnIqyN14d1z." }, "created": "2023-11-22T13:18:33.308Z", "expires": "2023-11-22T13:18:33.308Z", "last_used": "2023-11-22T13:18:33.308Z", "key": "string", "write_enabled": true, "description": "string" }

Observed Behavior

The request reaches the endpoint correctly:
def post(self, request): serializer = serializers.TokenProvisionSerializer(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) self.perform_create(serializer) return Response(serializer.data, status=HTTP_201_CREATED)

Error in data['key'] = Token.generate_key():
def to_internal_value(self, data): if 'key' not in data: data['key'] = Token.generate_key() return super().to_internal_value(data)
Detail:

File "/_/netbox/venv/lib/python3.10/site-packages/django/http/request.py", line 555, in __setitem__ self._assert_mutable() File "/_/netbox/venv/lib/python3.10/site-packages/django/http/request.py", line 552, in _assert_mutable raise AttributeError("This QueryDict instance is immutable") AttributeError: This QueryDict instance is immutable [22/Nov/2023 12:45:18] "POST /api/users/tokens/provision/ HTTP/1.1" 500 179114

Originally created by @Towmin84 on GitHub (Nov 22, 2023). ### NetBox version v3.6.5 ### Python version 3.10 ### Steps to Reproduce It does not work "Non-authenticated REST API endpoint via which a user may create a Token.": 1.- POST: /api/users/tokens/provision/ Example Value Schema 1: `{ "expires": "2023-11-22T13:18:33.129Z", "write_enabled": true, "description": "string", "username": "string", "password": "string" }` Example Value Schema 2: `{ "username": "string", "password": "string" }` ### Expected Behavior A 201 response like: ` { "id": 0, "url": "string", "display": "string", "user": { "id": 0, "url": "string", "display": "string", "username": "tTnthh-HVxpS.@a@fEMI+mkEa_.pCGXhb6ZcQKWJBITD1tL8UnCnIqyN14d1z." }, "created": "2023-11-22T13:18:33.308Z", "expires": "2023-11-22T13:18:33.308Z", "last_used": "2023-11-22T13:18:33.308Z", "key": "string", "write_enabled": true, "description": "string" } ` ### Observed Behavior The request reaches the endpoint correctly: ` def post(self, request): serializer = serializers.TokenProvisionSerializer(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) self.perform_create(serializer) return Response(serializer.data, status=HTTP_201_CREATED)` Error in data['key'] = Token.generate_key(): ` def to_internal_value(self, data): if 'key' not in data: data['key'] = Token.generate_key() return super().to_internal_value(data) ` Detail: ` File "/_/netbox/venv/lib/python3.10/site-packages/django/http/request.py", line 555, in __setitem__ self._assert_mutable() File "/_/netbox/venv/lib/python3.10/site-packages/django/http/request.py", line 552, in _assert_mutable raise AttributeError("This QueryDict instance is immutable") AttributeError: This QueryDict instance is immutable [22/Nov/2023 12:45:18] "POST /api/users/tokens/provision/ HTTP/1.1" 500 179114 `
adam added the type: bugstatus: revisions needed labels 2025-12-29 20:42:19 +01:00
adam closed this issue 2025-12-29 20:42:19 +01:00
Author
Owner

@abhi1693 commented on GitHub (Nov 22, 2023):

Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.6.5. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.

@abhi1693 commented on GitHub (Nov 22, 2023): Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.6.5. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.
Author
Owner

@jeremystretch commented on GitHub (Nov 22, 2023):

Please post the complete request you're sending to NetBox, including headers.

@jeremystretch commented on GitHub (Nov 22, 2023): Please post the _complete_ request you're sending to NetBox, including headers.
Author
Owner

@orionalves commented on GitHub (Dec 5, 2023):

I had the same problem.
If I call it from outside, it returns 405 (using insomnium) or returns nothing (using curl).
It only resolves when I call the api (using curl) on localhost. Then, I can call from outside again.
After a while of no use, it stops working and I need to call again from localhost for it to work.

@orionalves commented on GitHub (Dec 5, 2023): I had the same problem. If I call it from outside, it returns 405 (using insomnium) or returns nothing (using curl). It only resolves when I call the api (using curl) on localhost. Then, I can call from outside again. After a while of no use, it stops working and I need to call again from localhost for it to work.
Author
Owner

@jeremystretch commented on GitHub (Dec 5, 2023):

Again, without complete reproduction steps there's no way for us to troubleshoot this, or even identify that it's a problem with NetBox itself. I'll point out that we do test this endpoint as part of our CI process.

@jeremystretch commented on GitHub (Dec 5, 2023): Again, without complete reproduction steps there's no way for us to troubleshoot this, or even identify that it's a problem with NetBox itself. I'll point out that we do [test this endpoint](https://github.com/netbox-community/netbox/blob/9d09916f6e237a6d18c2e952e7a9da91f21d3381/netbox/users/tests/test_api.py#L172) as part of our CI process.
Author
Owner

@orionalves commented on GitHub (Dec 5, 2023):

localhost works, but external query does not.
External query doesn't work before I call it on localhost. You don't have an external test of your application.

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json; indent=2" https://netbox.mydomain.com/api/users/tokens/provision/ --data '{"username": "myuser","password": "my@Pass1"}'

I can't replicate the error behavior now as it is in production. But the api stops responding if I don't make queries for a long time.
So, I call it on localhost and everything works normally again.

netbox (v3.6.6)

@orionalves commented on GitHub (Dec 5, 2023): localhost works, but external query does not. External query doesn't work before I call it on localhost. You don't have an external test of your application. `curl -X POST -H "Content-Type: application/json" -H "Accept: application/json; indent=2" https://netbox.mydomain.com/api/users/tokens/provision/ --data '{"username": "myuser","password": "my@Pass1"}'` I can't replicate the error behavior now as it is in production. But the api stops responding if I don't make queries for a long time. So, I call it on localhost and everything works normally again. netbox (v3.6.6)
Author
Owner

@jeremystretch commented on GitHub (Dec 5, 2023):

@orionalves this sounds like a deployment issue, and likely not related to the original bug report. Note that the report indicates receiving a 500 error rather than a 405. And the API definitely should not be responding intermittently.

@jeremystretch commented on GitHub (Dec 5, 2023): @orionalves this sounds like a deployment issue, and likely not related to the original bug report. Note that the report indicates receiving a 500 error rather than a 405. And the API definitely should not be responding intermittently.
Author
Owner

@jeremystretch commented on GitHub (Dec 5, 2023):

I'm going to close this out as no one has been able to reproduce the reported behavior. If you are able to provide specific reproduction instructions, you are welcome to request that the issue be re-opened.

@jeremystretch commented on GitHub (Dec 5, 2023): I'm going to close this out as no one has been able to reproduce the reported behavior. If you are able to provide specific reproduction instructions, you are welcome to request that the issue be re-opened.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8866