Request & Response

In glim framework, requests and responses are handled by application controllers. There exists a Controller class in glim.component module that every controllers must extend. The request & response objects are just wrappers of Werkzeug's BaseRequest & BaseResponse.

Request

After a request is mapped into a controller, the request variables are automatically injected into Controller. Since all the application controllers must extend Controller class, it is not needed to set the request explicitly for each controller. The base controller class is the following;

class Controller:
    """

    The controller component is responsible for handling requests
    and returning appropriate response to the requests.

    Attributes
    ----------
      request (werkzeug.wrappers.Request): The current request
        object. This object is automatically passed by dispatch
        module.

    """
    def __init__(self, request):
        self.request = request

Simple, isn’t it? So, each controller can access the request object by self.request that is provided by werkzeug dispatcher. self.request is an alias of werkzeug.wrappers.BaseRequest object that is injected by dispatcher.

Some useful functions for Request are the following;

self.request.environ  # the wrapped wsgi environ dict
self.request.app      # bottle application object
self.request.route    # current Route object
self.request.url_args # arguments that are extracted from url
self.request.method   # request method of current route
self.request.headers  # request headers as dict
self.request.get_header(name, default=None) # retrieve header
self.request.cookies  # cookies parsed into FormsDict
self.request.get_cookie(key, default=None, secret=None) # retrieve cookie
self.request.query.<name>  # retrieve query string key
self.request.forms.<name>  # retrieve form variable
self.request.params.<name> # retrieve form & query combined vars
self.request.json # retrieve json body
self.request.body # retrieve raw body
self.request.is_xhr # flag of request triggered by XMLHttpRequest
self.request.is_ajax # flag of ajax
self.request.auth # http authentication data
self.request.remote_addr # remote ip address
self.request.remote_route # list of all ips with X-Forwarded-For header
self.request.copy() # make a shallow copy of request

A much more extensive documentation about Request objects can be found here

Response

The responses are the returned variables of any application Controller class in glim. For example;

# controllers.py
from app.models import Post
from glim import Controller
from glim import Response
from glim_extensions.db import Orm

import json

class PostController(Controller):
    def get(id):
        response = {}
        response['error'] = None
        return response
        # returns {"error": null}

This is a simple REST like web service example of a PostController. Notice that, Response is a type of werkzeug.wrappers.BaseResponse. You can also drop the use of return Response() statement and use only return json.dumps(response). Glim will understand this and create a Response for you.

🚧

Do not use string based returns in filters

Although glim can convert the string returns into Responses, it is highly suggested not to use string base returns in filters for not having ambiguous definitions.

Some useful variables for generating response;

variabledescription
headersa list of headers or a Headers object
status_codethe http status code of response
mimetypethe mimetype for the request
content_typethe content type for the request
get_header(name, default=None)retrieve header key
set_header(name, value)set header key - value
add_header(name, value)add header key - value
content_typeshortcut of Content-Type

A much more extensive documentation of Response objects can be found here