简体   繁体   中英

Send user data in python requests module

I am unable to send the user details along with requests module i had to hard code the user details in the data payload to identify the user.

    full_url = ''.join(['http://', get_current_site(request).domain, '/am/reply'])
    data = {    
               'agent_type':'trigger',
               'input':platform,
               'userid':request.user.id ####==>> had to send userid like this
           } 
    a = requests.get(full_url,params=data)

Is there way to send all general request data using requests.?

And moreover the requests url the destination view i have implemented

def index(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('login'))

And request.user.id is none when url is reached through requests module In general how should i validate a request when using requests module

Django uses request and response objects to pass state through the system.

When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.

Some of the middleware included in Django's contrib apps set attributes on the request. If you don't see the attribute on a request, be sure the appropriate middleware class like authenticationmiddleware,sessionmiddleware.

Following piece of code will give the user.id if and only if the user is authenticated.

def myview(request):
    if request.user.is_authenticated:
        print request.user.id
    else:
        ... # Do something else.

https://docs.djangoproject.com/en/1.10/ref/request-response/

If I understood your question correctly, You are getting request in one view, and then making a call to other view using requests module. It that case the request object in index view will be totally different because that request was sent from your server where application works, not from user. You can only get data in index view using request.GET.get("userid") and so on. And then if you will need user info, just fetch it again from database using userid. Passing request object to other view using requests library is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM