watson.http.messages

class watson.http.messages.MessageMixin[source]

Base mixin for all Http Message objects.

class watson.http.messages.Request(environ)[source]

Provides a simple and usable interface for dealing with Http Requests. Requests are designed to be immutable and not altered after they are created, as such you should only set get/post/cookie etc attributes via the __init__. By default the session storage method is MemoryStorage which will store session in ram.

See:

Example:

request = Request.from_environ(environ)
print(request.method)
print(request.post('my_post_var'))

request = Request.from_dicts(server={'HTTP_METHOD': 'GET'}, get={'get_var': 'somevalue'})
print(request.method) # get
print(request.get('get_var')) # somevalue
__init__(environ)[source]
body

Return the body of the request as a string.

If unable to decode, return empty body.

host()[source]

Determine the real host of a request.

Returns:X_FORWARDED_FOR header variable if set, otherwise a watson.http.uri.Url hostname attribute.
is_method(*methods)[source]

Determine whether or not a request was made via a specific method.

Example:

request = ... # request made via GET
request.is_method('get') # True
Parameters:method (string|list|tuple) – the method or list of methods to check
Returns:Boolean
is_secure()[source]

Determine whether or not the request was made via Https.

Returns:Boolean
is_xml_http_request()[source]

Determine whether or not a request has originated via an XmlHttpRequest, assuming the relevant header has been set by the request.

Returns:Boolean
json_body

Returns the body encoded as JSON.

post

A dict of all POST variables associated with the request.

Returns:A dict of POST variables
class watson.http.messages.Response(status_code=None, headers=None, body=None, version=None)[source]

Provides a simple and usable interface for dealing with Http Responses.

See:

Example:

def app(environ, start_response):
    response = Response(200, body='<h1>Hello World!</h1>')
    response.headers.add('Content-Type', 'text/html', charset='utf-8')
    response.cookies.add('something', 'test')
    start_response(*response.start())
    return [response()]
__init__(status_code=None, headers=None, body=None, version=None)[source]
Parameters:
  • status_code (int) – The status code for the Response
  • headers (watson.http.headers.HeaderCollection) – Valid response headers.
  • body (string) – The content for the response
  • version (string) – The Http version for the response
body

Returns the decoded body based on the response encoding type.

cookies

Returns the cookies associated with the Response.

encoding

Retrieve the encoding for the response from the headers, defaults to UTF-8.

raw()[source]

Return the raw encoded output for the response.

start()[source]

Return the status_line and headers of the response for use in a WSGI application.

Returns:The status line and headers of the response.
status_code

The status code for the Response.

status_line

The formatted status line including the status code and message.