TypeError: 'Request' object does not support item assignment
See original GitHub issueI’m creating an API gateway using fastAPI for some microservices, which in I authenticate user via Azure AD via this library. So I’m trying to take out the user info from the request object (request.state.user) and inject my own token in it to later pass to other microservices. I can’t do this in middleware, cause user info is getting added after it. But I tried creating an decorator:
from functools import wraps
def token_injector(function):
@wraps(function)
def wrap_function(*args, **kwargs):
user: User = kwargs['request'].state.user
new_token = generate_token(user)
kwargs["new_token"] = new_token # or kwargs['request']["new_token"] = new_token
return function(*args, **kwargs)
return wrap_function
and for this approach, I’m getting these errors:
ValueError: [TypeError("'coroutine' object is not iterable"), TypeError('vars() argument must have dict attribute')]
and
TypeError: 'Request' object does not support item assignment
Can you please guide me how did you inject user in request.state, so I can do the same?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Object does not support item assignment error - Stack Overflow
The error seems clear: model objects do not support item assignment. MyModel.objects.latest('id')['foo'] = 'bar' will throw this same error.
Read more >TypeError: 'type' object does not support item assignment
The python error TypeError: 'type' object does not support item assignment occurs when an index value is inserted or changed in a variable...
Read more >'Request' object does not support item assignment #36 - GitHub
Sanic no longer allows for item assignment in the request object. I am using sanic==20.6.3 and sanic-prometheus==0.2.0. ERROR:sanic.root: ...
Read more >'Response' object does not support item assignment - mkt.api ...
This error used to happen when the returned response is not a Django response (but a dict or a list for instance). I...
Read more >Tuple Object Does Not Support Item Assignment. Why?
The error “tuple object does not support item assignment” is raised in Python when you try to modify an element of a tuple....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi! Thank you for following up letting me know it’s been resolved. I don’t have enough reputation on SO to follow up there.
There are a few questions here, so I’ll try to answer them as best as I can. Let me know if anything is unclear.
First off, I agree with the approach you’ve landed on. Chaining dependencies is the way to make sure something is “in the right order”. It is also the approach I’ve described in the docs here, and also deomonstrated in the demo project in this view, using this dependency.
The user model is defined here, and is created here.
I would recommend you to not “get rid of” the
User
object, but maybe create your own user model and append to it. Here’s an example:and then in your own dependency do this:
I hope this code runs (I haven’t been able to test it), but if not, let me know and I’ll write a real example for you tomorrow.
Ah, yes, I forgot about that part of the question 👍