Context object reference guide
The context
object has several properties that may be accessed by the function developer.
log
Provides a logging object that can be used to write output to the cluster logs. The log adheres to the Pino logging API (getpino.io/#/docs/api).
Function myFunction(context) {
context.log.info(“Processing customer”);
}
Access the function via curl to invoke it:
curl http://example.com
The function will log:
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
query
Returns the query string for the request, if any, as key value pairs. These attributes are also found on the context object itself.
Function myFunction(context) {
// Log the 'name' query parameter
context.log.info(context.query.name);
// Query parameters also are attached to the context
context.log.info(context.name);
}
Access the function via curl to invoke it:
curl http://example.com?name=tiger
The function will log:
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
body
Returns the request body if any. If the request body contains JSON code, this will be parsed so that the attributes are directly available.
Function myFunction(context) {
// log the incoming request body's 'hello' parameter
context.log.info(context.body.hello);
}
Access the function via curl to invoke it:
curl -X POST -d '{"hello": "world"}' -H'Content-type: application/json' http://example.com
The function will log:
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
headers
Returns the HTTP request headers as an object.
Function myFunction(context) {
context.log.info(context.headers["custom-header"]);
}
Access the function via curl to invoke it:
curl -H'x-custom-header: some-value’' http://example.com
The function will log:
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}