Response class

All requests need a response, the response class creates an object for every request, the one can be used to send custom headers or HTTP status codes.

The second argument for the dispatch method is the response object:

1
2
3
4
5
6
7
from zunzuncito import tools

class APIResource(object):


    def dispatch(self, request, response):
        """ your code goes here """

Response object contents

Name Description
log logger intance.
request_id The request id.
headers A CaseInsensitiveDict instance, for storing the headers.
status Default 200 an int respresenting an HTTP status code.
start_response The start_response() Callable.
extra A list for repeated headers used with the add_header method

add_header

If you need to create multiple headers using the same key for example to set up cookies you should use the add_header method.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from Cookie import SimpleCookie
from zunzuncito import tools


class APIResource(object):

    def __init__(self):
        self.headers['Content-Type'] = 'text/html; charset=UTF-8'

    def dispatch(self, request, response):

        response.headers.update(self.headers)

        cookie = SimpleCookie()
        cookie.load(request.environ['HTTP_COOKIE'])

        cookie['session'] = session
        cookie["session"]["path"] = "/"
        cookie["session"]["expires"] = 12 * 30 * 24 * 60 * 60
        for morsel in cookie.values():
            response.add_header('Set-Cookie', morsel.OutputString())

        try:
            name = request.path[0]
        except Exception:
            name = ''

       if name:
             return 'Name: ' + name

        response.status =  406
        """ print all headers """
        print str(response)
        return []

Many thanks Paw - The ultimate REST client for Mac. for supporting Open Source projects.

paw

A great amount of time has been spent creating, crafting and maintaining this software, please consider donating.

Donating helps ensure continued support, development and availability.

dalmp


comments powered by Disqus