Request Object
When your function is invoked, it receives a request
object containing detailed information about the incoming HTTP request. This object is structured as follows:
Properties
- method (string): The HTTP method used for the request (e.g., GET, POST, PUT, DELETE).
- path (string): The full URL path that triggered the function.
- params (object): An object containing query parameters included in the URL.
- headers (object): An object containing HTTP headers included with the request.
- body (string | object): The body of the request. Its type depends on the Content-Type header:
- For
application/json
, it will be a parsed JSON object. - For
application/x-www-form-urlencoded
ormultipart/form-data
, it will be an object representing the form fields. - For other content types, it will be a string containing the raw body content.
- For
Example
Here's an example of what a request
object might look like:
{
"method": "POST",
"path": "/api/v1/users",
"params": {
"include_details": "true"
},
"headers": {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
},
"body": {
"username": "johndoe",
"email": "johndoe@example.com"
}
}
Notes
- The
body
content is automatically parsed based on theContent-Type
header. - For
application/json
content, the JSON is parsed into an object. - For form data (
application/x-www-form-urlencoded
ormultipart/form-data
), the form fields are parsed into an object. - For other content types, the body is provided as a raw string.
- File uploads are not supported and will result in an error response.
- The maximum allowed request body size is 100 KB (102,400 bytes).
Response Handling
When your function processes the request and is ready to return a response, the response is structured based on what your function returns:
-
Returning Nothing: If your function doesn't return anything or returns null, a response with a status code of 204 (No Content) will be generated, indicating that the request was successful but there is no content to return.
-
Returning a String: If your function returns a simple string, this string will be treated as the body of the response with a status code of 200 (OK). The content type will be set to
text/plain
by default. -
Returning an Object or Array: If your function returns an object or array, it will be serialized into JSON and returned with a status code of 200 (OK). The content type will be set to
application/json
. -
Custom Responses: If you need to customize the response further (e.g., setting a specific status code, headers, or content type), you can return a dictionary with the following keys:
- statusCode: The HTTP status code (e.g., 200 for OK, 404 for Not Found).
- headers: A dictionary of headers to include in the response.
- body: The content of the response, which can be a string or JSON-serializable data.
Example Usage
Here’s how you might structure your function to return different types of responses:
-
Simple String Response:
def handler(request): return "Hello, World!"
-
JSON Response:
def handler(request): return {"message": "Hello, World!"}
-
Custom Response:
def handler(request): return { "statusCode": 404, "headers": {"Content-Type": "text/plain"}, "body": "Resource not found" }
These options give you the flexibility to handle responses in the way that best fits your application's needs.