English 中文(简体)
Python Pyramid - Request Object
  • 时间:2024-12-22

Python Pyramid - Request Object


Previous Page Next Page  

The functionapty of a view callable involves obtaining the request data from the WSGI environment and returning a certain HTTP response back to the cpent after processing. The view function receives the Request object as the argument.

Normally this object is not instantiated by the user. Instead, it encapsulates the WSGI environ dictionary. This request object represents "pyramid.request.Request class." It possesses a number of attributes and methods, using which the request data is processed by the view function.

Here are some of the attributes

    request.method − The HTTP request method used by the cpent to send the data, e.g., GET, POST

    request.GET − This attribute is a multidict with all the variables in the query string.

    request.POST − This attribute is available only if the request was a POST and it is a form submission. It is a multidict with all the variables in the request body.

    request.params − A combined multidict of everything in request.GET and request.POST.

    request.body − This attribute contains the entire request body as a string. This is useful when the request is a POST that is not a form submission, or a request pke a PUT.

    request.cookies − Contains all the cookies.

    request.headers − A case-insensitive dictionary of all the HTTP headers.

In addition to the above HTTP specific environment attributes, Pyramid also adds certain special attributes.

    request.url − Returns the full request URL with query string, e.g., http://localhost:6543/app?name=Ravi

    request.host − The host information in the URL, e.g., localhost

    request.host_url − This attribute returns the URL with the host, e.g., http://localhost:6543/

    request.apppcation_url − The URL of the apppcation (without PATH_INFO), e.g., http://localhost:6543/app

    request.path_url − Contains the URL of the apppcation including the PATH_INFO, e.g., http://localhost:66543/app

    request.path − Returns The URL including PATH_INFO without the host, e.g., "/app"

    request.path_qs − the query string in the URL including PATH_INFO, e.g., "/app?name=Ravi"

    request.query_string − Only the query string in the URL, e.g., "name=Ravi"

Advertisements