Pyramid causing an additional request

I am trying to implement a batch request method in a pyramid. I see in the docs that this is done using

subrequest = Request.blank('/relative/url')
request.invoke_subrequest(subrequest)

I'm just wondering how you go through the headers and cookies? Is it already done for you or is it

request.invoke_subrequest(subrequest, cookies=request.cookies, headers=request.headers)

What about parameters for different methods? Documents have only <keyword POST.

It seems to me that the documents are a bit vague, or I cannot find the correct documents on how to do this. Thanks

+4
source share
3 answers

I'm just wondering how you go through the headers and cookies?

From http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/subrequest.html#subrequest-chapter :

API pyramid.request.Request.invoke_subrequest() : , , use_tweens, ; False.

, Request use_tweens, ,

request.invoke_subrequest(subrequest, cookies=request.cookies, headers=request.headers)

.

http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/subrequest.html

invoke_subrequest(). , , pyramid.request.Request.blank(). , , . URL , , . pyramid.request.Request , , , , , -, , .

, , invoke_subrequest().

, , . ..


? POST.

REQUEST_METHOD .

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/viewconfig.html

request_method ( "GET", "POST", "PUT", "DELETE" "HEAD" ), HTTP REQUEST_METHOD

, , , :

request.method = 'POST'
# Or
request.method = 'GET'
# Etc.

,

subrequest = Request.blank('/relative/url')
# Configure the subrequest object
# set headers and other options you need.
request.invoke_subrequest(subrequest)

, 100% , , ( ), , , , , , .

+2

. (, cookie, , ..):

def subrequest(request, path):
    subreq = request.copy()
    subreq.path_info = path
    response = request.invoke_subrequest(subreq)
    return response
+3

, . , . blank(), kw,

, , , . base_url, wsgi.url_scheme, HTTP_HOST SCRIPT_NAME.

, cookie, , :

@view_config( ... )
def something(request):
    ...
    kwargs = { "cookies": request.cookies,                                  
               "host"   : request.host,                                     
             }                                                              
    req = Request.blank("/relative/url", **kwargs)
    resp = request.invoke_subrequest(req)                                   

Other header information (e.g. accept, accept_encodingetc.) are properties of objects pyramid.request and can be added to the dictionary kwargs, as shown in the above code fragment.

The object returned invoke_subrequest()is the Response object documented here .

0
source

Source: https://habr.com/ru/post/1540781/


All Articles