Pyjo.Message.Request - HTTP request

import Pyjo.Message.Request

# Parse
req = Pyjo.Message.Request.new()
req.parse(b"GET /foo HTTP/1.0\x0d\x0a")
req.parse(b"Content-Length: 12\x0d\x0a")
req.parse(b"Content-Type: text/plain\x0d\x0a\x0d\x0a")
req.parse(b'Hello World!')
print(req.method)
print(req.headers.content_type)
print(req.body)

# Build
req = Pyjo.Message.Request.new()
req.url.parse('http://127.0.0.1/foo/bar')
req.method = 'GET'
print(req)

Pyjo.Message.Request is a container for HTTP requests based on RFC 7230, RFC 7231, RFC 7235 and RFC 2817.

Events

Pyjo.Message.Request inherits all events from Pyjo.Message.

Classes

class Pyjo.Message.Request.Pyjo_Message_Request(**kwargs)

Pyjo.Message.Request inherits all attributes and methods from Pyjo.Message and implements the following new ones.

__repr__()
string = repr(req)

String representation of an object shown in console.

clone()
clone = req.clone

Clone request if possible, otherwise return None.

cookies
cookies = req.cookies
req.cookies = cookies

Access request cookies, usually Pyjo.Cookie.Request objects.

# Names of all cookies
for cookie in req.cookies:
    print(cookie.name)
environ = None
environ = req.environ
req.environ = {}

Direct access to the CGI or WSGI environment hash if available.

# Check CGI version
version = req.environ['GATEWAY_INTERFACE']

# Check WSGI version
version = req.environ['wsgi.version']
every_param(name)
values = req.every_param('foo')

Similar to param(), but returns all values sharing the same name as an array reference.

# Get first value
print(req.every_param('foo')[0])
extract_start_line(buf)
boolean = req.extract_start_line(buf)

Extract request-line from string.

fix_headers()
req = req.fix_headers()

Make sure request has all required headers.

get_start_line_chunk(offset)
chunk = req.get_start_line_chunk(offset)

Get a chunk of request-line data starting from a specific position.

is_handshake
boolean = req.is_handshake

Check Upgrade header for websocket value.

is_secure
boolean = req.is_secure

Check if connection is secure.

is_xhr
boolean = req.is_xhr

Check X-Requested-With header for XMLHttpRequest value.

method = None
method = req.method
req.method = 'POST'

HTTP request method, defaults to GET.

param(name, value=None)
value = req.param('name')
value = req.param('name', 'value')

Access GET and POST parameters extracted from the query string and application/x-www-form-urlencoded or multipart/form-data message body. If there are multiple values sharing the same name, and you want to access more than just the last one, you can use every_param(). Note that this method caches all data, so it should not be called before the entire request body has been received. Parts of the request body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large, there’s a 16MB limit by default.

params
params = req.params

All GET and POST parameters extracted from the query string and application/x-www-form-urlencoded or multipart/form-data message body, usually a Pyjo.Parameters object. Note that this method caches all data, so it should not be called before the entire request body has been received. Parts of the request body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large, there’s a 16MB limit by default.

# Get parameter names and values
params_dict = req.params.to_dict()
parse(request)
req = req.parse(b'GET /foo/bar HTTP/1.1')
req = req.parse({'REQUEST_METHOD': 'GET'})

Parse HTTP request chunks or environment dict.

proxy
proxy = req.proxy
req.proxy = 'http://foo:bar@127.0.0.1:3000'
req.proxy = Pyjo.URL.new('http://127.0.0.1:3000')

Proxy URL for request.

# Disable proxy
req.proxy = None
query_params
params = req.query_params

All GET parameters, usually a Pyjo.Parameters object.

# Turn GET parameters to hash and extract value
print(req.query_params.to_hash()['foo'])
reverse_proxy = None
boolean = req.reverse_proxy
req.reverse_proxy = boleean

Request has been performed through a reverse proxy.

req = req.set_cookie(cookie, cookie, cookie)
req = req.set_cookie(Pyjo.Message.Response.new(name='foo', value='bar'))
req = req.set_cookie({'name': 'foo', 'value': 'bar'})

Set message cookies, usually Pyjo.Cookie.Response object.

to_bytes()
bstring = req.to_bytes()

Turn message into a bytes string.

url = None
url = req.url
req.url = Pyjo.URL.new()

HTTP request URL, defaults to a Pyjo.URL object.

# Get request information
info = req.url.to_abs().userinfo
host = req.url.to_abs().host
path = req.url.to_abs().path
Pyjo.Message.Request.object

alias of Pyjo_Message_Request