Skip to main content
Version: LOC v0.6 (legacy)

Payload

Payload is the data sent by triggers as input and is accessible from the context object. Currently there are two payload types:

  • HTTP request body
  • MQ message

Availability

  • ✓ Generic logic
  • ✓ Aggregator logic
info

Since the payload type depends on triggers, developers using a TypeScript template in CLI must use a type guard to ensure type safety in the compiling process:

let data: number[] = [];

if ("http" in ctx.payload) {
// check if it's HTTP payload
data = ctx.payload.http.body;
} else if ("messageQueue" in ctx.payload) {
// check if it's MQ payload
data = ctx.payload.messageQueue.data;
} else {
// if none of above, throw an error
throw new Error("this logic only accepts http/mq payload");
}

// data will be the payload content

HTTP Payload

ctx.payload.http (type: HttpPayload)

MemberTypeDescriptionExample
apiGatewayIdentityContextDataSourceIdentityContext, which is { id: string, name: string }API gateway permanent ID and name
apiIdentityContextDataSourceIdentityContextAPI route permanent ID and name
requestIdstringRequest ID
hoststringRequest host name
pathstringAPI route/path/subpath
schemestringHTTP schemehttp or https
methodstringHTTP methodGET, POST, etc.
version"HTTP/0.9" | "HTTP/1.0" | "HTTP/1.1" | "HTTP/2.0" | "HTTP/3.0"HTTP version
headers{ [k: string]: unknown; }Request headers{ "content-type": "application/json" }
querystringURL query stringparam1=value1 (no question mark)
bodynumber[]Request body

Example: read API route and headers

const path = ctx.payload.http.path;
const headers = ctx.payload.http.headers;
const contentType = headers["content-type"];

Example: parsing GET querystring

// assume the querystring is ?name=<name>&age=<age>

const query = ctx.payload.http.query;

const searchParams = new URLSearchParams(query);

// access parsed fields (return null if not exist)
const name = searchParams.get("name");
const age = searchParams.get("age");

Example: parsing POST JSON body

// assume the payload is { "name": <name>, "age": <age> }

const body = new TextDecoder().decode(new Uint8Array(ctx.payload.http.body));

const parsed = JSON.parse(body);

// access parsed fields using optional chaining (return undefined if not exist)
const name = parsed?.name;
const age = parsed?.age;

Message Queue Payload

ctx.payload.messageQueue (type: MessageQueuePayload)

MemberTypeDescription
clientIdentityContextDataSourceIdentityContext, see HTTP payloadMQ client permanent ID and name
datanumber[]MQ data
subscriberSubscriberMQ subscriber

Subscriber type members describe the message queue sources:

  • Subscriber type: KafkaSubscriber
MemberTypeDescription
brokersstring[]Broker names in the Kafka cluster
groupIdstringMQ group ID
topicstringMQ topic
partitionnumberMQ partition
offsetnumberMQ offset
tip

Supports for other message queues are on the way!