dispatch method¶
The dispatch
method belongs to the APIResource
class and is called by the ZunZun instance in
order to process the requests.
Basic template¶
1 2 3 4 5 6 7 | from zunzuncito import tools
class APIResource(object):
def dispatch(self, request, response):
""" your code goes here """
|
Status codes¶
The default HTTP status code is 200, but based on your needs you can change it to fit you response very eazy by just doing something like:
response.status = 201
Headers¶
As with the status codes, same happens with the HTTP headers, The default headers are:
Content-Type: 'application/json; charset=UTF-8'
Request-ID: <request_id>
For updating/replacing you just need to do something like:
response.headers['my_custom_header'] = str(uuid.uuid4())
Example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from zunzuncito import tools
class APIResource(object):
def __init__(self, api):
self.headers = {'Content-Type': 'text/html; charset=UTF-8'}
def dispatch(self, request, response):
try:
name = self.api.path[0]
except:
name = ''
if name:
response.headers['my_custom_header'] = name
else:
response.status = 406
response.headers.update(self.headers)
return 'Name: ' + name
|
The output for:
curl -i http://api.zunzun.io/status_and_headers
1 2 3 4 5 6 7 8 9 10 11 | HTTP/1.1 406 Not Acceptable
Request-ID: 52e78a1500ff0f217359e91eb90001737e7a756e7a756e6369746f2d617069000131000100
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding
Date: Tue, 28 Jan 2014 10:44:38 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked
Name:
|
The output for:
curl -i http://api.zunzun.io/status_and_headers/foo
1 2 3 4 5 6 7 8 9 10 11 12 | HTTP/1.1 200 OK
Request-ID: 52e78a9300ff0f3fe44a7e4fbf0001737e7a756e7a756e6369746f2d617069000131000100
my_custom_header: foo
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding
Date: Tue, 28 Jan 2014 10:46:44 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked
Name: foo
|
See also
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.
comments powered by Disqus