Skip to main content

HTTP Payload - JS/TS

The logic payload type when a task is invoked by an API Route trigger.

Class Reference

Type

  • { http: HttpPayload } (one of the Payload type)

Sub Class Reference

HttpPayload

Importable from @fstnetwork/loc-logic-sdk

PropertyTypeDescription
apiGatewayIdentityContextIdentityContextFor_UuidAPI gateway permanent ID and name
apiIdentityContextIdentityContextFor_UuidAPI route permanent ID and name
requestIdstringRequest ID
requestHttpRequestRequest content
source?Peer | nullRequest source
destination?Peer | nullRequest destination

IdentityContextFor_Uuid

PropertyTypeDescription
idstringAPI gateway/route ID
namestringAPI gateway/route name

Peer

PropertyTypeDescription
addressAddressSee below

Type Address is

{
socketAddr: {
address: string;
protocol: "tcp" | "udp";
}
} | {
pipe: {
mode: number;
path: string;
}
}

HttpRequest

Data and metadata of the HTTP request.

PropertyTypeDescriptionExample
hoststringRequest host name
pathstringAPI route/api/path
schemestringHTTP schemehttp, https
methodstringHTTP methodGET, POST, etc.
versionHTTP/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" }
querystringquerystringparam1=value1 (no question mark; empty string if no querystring available)
datanumber[]Request body (empty array if not available)
info

HttpRequest.data would be an Uint8Array array anc can be convert to string using Web API TextDecoder.

Examples

Parse Querystring

Assuming the querystring is

`?name=Arthur+Dent&age=42`

The parameters can be parsed using Web API URLSearchParams:

const payload = await ctx.payload();

const queryParams = Object.fromEntries(
new URLSearchParams(payload.http.request.query)
);

// get values (undefined if not exist)
const name = queryParams.name;
const age = queryParams.age;

// or
// const name = queryParams["name"];
// const age = queryParams["age"];

Parse HTTP Payload body to JSON

Assuming the HTTP request includes a body with the following JSON string:

{
"name": "Arthur Dent",
"age": 42,
"job": {
"title": "Sandwich-maker",
"salary": 0
},
"quotes": [
"Is there any tea on this spaceship?",
"This must be Thursday. I never could get the hang of Thursdays.",
"Would it save you a lot of time if I just gave up and went mad now?"
]
}
const payload = await ctx.payload();

const data = payload.http?.request.data;

let parsed;
if (data) {
parsed = JSON.parse(new TextDecoder().decode(new Uint8Array(data)));
}

// get values (unknown if not exist)
const name = parsed?.name;
const age = parsed?.age;
const title = parsed?.job?.title;

// or
// const name = parsed?.["name"];
// const age = parsed?.["age"];
// const title = parsed?.["job"]?.["title"];

const quotes = parsed?.quotes;
const firstQuote = quotes ? quotes[0] : null;
for (const quote of quotes) {
// ...
}