Simple JWT TokenRefreshView: response has two types
I'm having trouble accessing the data attribute on a Response object in Django Rest Framework after refreshing the access token.
factory = APIRequestFactory()
new_request = factory.post(
"/api/token/refresh/",
{"refresh": refresh_token},
format="json",
)
new_request.META["CONTENT_TYPE"] = "application/json"
refresh_view = TokenRefreshView.as_view()
refresh_response = refresh_view(new_request)
print(type(refresh_response))
refresh_response.data["message"] = "Token refresh successful"
When I run this code, the print statement correctly outputs <class 'rest_framework.response.Response'>
.
However, Pylint is complaining that I cannot access the data attribute.
Cannot access attribute "data" for class "HttpResponse" Attribute "data" is unknownPylancereportAttributeAccessIssue
Response from TokenRefreshView.as_view()
is an HTTPResponse object, or a subclass of it like Response from DRF.
Convert response to JSON and use it.
import json
if refresh_response.status_code == 200:
response_data = json.loads(refresh_response.content)
response_data["message"] = "Token refresh successful"
else:
print("Token refresh failed with status code: ", refresh_response.status_code)