// Module included in the following assemblies: // // * serverless/functions/serverless-functions-reference-guide.adoc :_mod-docs-content-type: REFERENCE [id="serverless-typescript-context-object-reference_{context}"] = TypeScript context object reference The `context` object has several properties that can be accessed by the function developer. Accessing these properties can provide information about incoming HTTP requests and write output to the cluster logs. [id="serverless-typescript-context-object-reference-log_{context}"] == log Provides a logging object that can be used to write output to the cluster logs. The log adheres to the link:https://getpino.io/#/docs/api[Pino logging API]. .Example log [source,javascript] ---- export function handle(context: Context): string { // log the incoming request body's 'hello' parameter if (context.body) { context.log.info((context.body as Record).hello); } else { context.log.info('No data received'); } return 'OK'; } ---- You can access the function by using the `kn func invoke` command: .Example command [source,terminal] ---- $ kn func invoke --target 'http://example.function.com' ---- .Example output [source,terminal] ---- {"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"} ---- You can change the log level to one of `fatal`, `error`, `warn`, `info`, `debug`, `trace`, or `silent`. To do that, change the value of `logLevel` by assigning one of these values to the environment variable `FUNC_LOG_LEVEL` using the `config` command. [id="serverless-typescript-context-object-reference-query_{context}"] == query Returns the query string for the request, if any, as key-value pairs. These attributes are also found on the context object itself. .Example query [source,javascript] ---- export function handle(context: Context): string { // log the 'name' query parameter if (context.query) { context.log.info((context.query as Record).name); } else { context.log.info('No data received'); } return 'OK'; } ---- You can access the function by using the `kn func invoke` command: .Example command [source,terminal] ---- $ kn func invoke --target 'http://example.function.com' --data '{"name": "tiger"}' ---- .Example output [source,terminal] ---- {"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"} ---- [id="serverless-typescript-context-object-reference-body_{context}"] == 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. .Example body [source,javascript] ---- export function handle(context: Context): string { // log the incoming request body's 'hello' parameter if (context.body) { context.log.info((context.body as Record).hello); } else { context.log.info('No data received'); } return 'OK'; } ---- You can access the function by using the `kn func invoke` command: .Example command [source,terminal] ---- $ kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}' ---- .Example output [source,terminal] ---- {"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"} ---- [id="serverless-typescript-context-object-reference-headers_{context}"] == headers Returns the HTTP request headers as an object. .Example header [source,javascript] ---- export function handle(context: Context): string { // log the incoming request body's 'hello' parameter if (context.body) { context.log.info((context.headers as Record)['custom-header']); } else { context.log.info('No data received'); } return 'OK'; } ---- You can access the function by using the `curl` command to invoke it: .Example command [source,terminal] ---- $ curl -H'x-custom-header: some-value’' http://example.function.com ---- .Example output [source,terminal] ---- {"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"} ---- [id="serverless-typescript-context-object-reference-http-requests_{context}"] == HTTP requests method:: Returns the HTTP request method as a string. httpVersion:: Returns the HTTP version as a string. httpVersionMajor:: Returns the HTTP major version number as a string. httpVersionMinor:: Returns the HTTP minor version number as a string.