Request
By default, every endpoint function will receive an instance of the Request class (aka request) as the first
argument of their function. This request has a lot of properties which will do common things automatically, but
still allows the developer to override those operations if they deem necessary. Below is a list and examples of all
the properties of the request:
Request Properties
| property |
type |
mutable |
description |
method |
str |
no |
the http method of the request |
cookies |
list |
no |
the cookies of the request |
protocol |
str |
no |
the protocol of the request |
content_type |
str |
no |
the content_type of the request body |
host_url |
str |
no |
the host_url of the request was sent to |
domain |
str |
no |
the domain of the request was sent to |
stage |
str |
no |
the stage the lambda was deployed to |
resource |
str |
no |
the AWS resource being invoked |
authorizer |
object |
no |
if using a customized authorizer, the authorizer object |
headers |
object |
no |
the headers of the request |
params |
object |
no |
combination of query string and path params in one object |
query_params |
object |
no |
query string parameters from the request |
path_params |
object |
no |
the path parameters of the request |
route |
str |
no |
the requested route with placeholders of params |
path |
str |
no |
the raw requested path with actual param values |
json |
object |
no |
the body of the request, converted from json string in object |
xml |
object |
no |
the body of the request, converted from xml string in object |
graphql |
str |
no |
the body of the graphql request as a string |
body |
any |
no |
the body of the request, converted to based on data type |
raw |
any |
no |
the raw body of the request no conversion |
context |
object |
yes |
mutable request context to assigned and pass around |
request.cookies
| print(request.cookies)
# output:
['some-cookie']
|
request.protocol
| print(request.protocol)
# output:
'https'
|
request.content_type
| print(request.content_type)
# output:
'application/json'
|
request.host_url
| print(request.host_url)
# output:
'https://api.are-great.com'
|
request.domain
| print(request.domain)
# output:
'api.are-great.com'
|
request.stage
| print(request.stage)
# output:
'prod'
|
request.method
| print(request.method)
# output:
'get'
|
request.resource
| print(request.resource)
# output:
'/{proxy+}'
|
request.authorizer
Tip
This is only useful if you are using an external authorizer with your lambda.
| print(request.authorizer)
# output:
{
'apiKey': 'SOME KEY',
'userId': 'x-1-3-4',
'correlationId': 'abc12312',
'principalId': '9de3f415a97e410386dbef146e88744e',
'integrationLatency': 572
}
|
| print(request.headers)
# output:
{
'x-api-key': 'SOME-KEY',
'content-type': 'application/json'
}
|
request.params
Info
This combines both path parameters and query string parameters, nested in one object.
| print(request.params)
# output:
{
'query': {
'name': 'me'
},
'path': {
'id': 1
}
}
|
request.query_params
| print(request.query_params)
# output:
{
'name': 'me'
}
|
request.path_params
| print(request.path_params)
# output:
{
'id': 1
}
|
request.route
Info
This will provide the route with the path param variables included
| print(request.route)
# output:
'grower/{id}'
|
request.path
Info
This will provide the route with the path param values replacing the variables
| print(request.path)
# example output:
'grower/1'
|
request.json
Warning
This will raise an unhandled exception if the body is not json compatible
| print(request.json);
# output:
{
'some_json_key': 'some_json_value'
}
|
| print(request.form);
# output:
{
'some_form_key': 'some_form_value'
}
|
request.xml
Warning
This will raise an unhandled exception if the body is not xml compatible
| python(request.xml);
# output:
{
'some_xml_key': 'some_xml_value'
}
|
request.graphql
Info
This is graphql string since there is no object equivalent; you can pass this directly to your graphql resolver
| python(request.graphql);
# output:
'{
players {
name
}
}'
|
request.body
Tip
This is the safest way to get the body of the request. It will use the content-type header to determine the data sent and convert it; if the data can't be converted for whatever reason it will catch the error and return the raw body provided unconverted.
| print(request.body)
# output:
{
'some_key': 'some_value'
}
|
request.raw
| print(request.raw)
# output:
# whatever the raw data of the body is; string, json string, xml, binary, etc
|
request.context
Tip
This is the only mutable property of the request, to be used by any of the before or before_all middleware options
| request.context = {'application_assignable': true}
print(request.context)
# output:
{
'application_assignable': true
}
|