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
Property | Type | Description |
---|---|---|
apiGatewayIdentityContext | IdentityContextFor_Uuid | API gateway permanent ID and name |
apiIdentityContext | IdentityContextFor_Uuid | API route permanent ID and name |
requestId | string | Request ID |
request | HttpRequest | Request content |
source? | Peer | null | Request source |
destination? | Peer | null | Request destination |
IdentityContextFor_Uuid
Property | Type | Description |
---|---|---|
id | string | API gateway/route ID |
name | string | API gateway/route name |
Peer
Property | Type | Description |
---|---|---|
address | Address | See below |
Type Address
is
{
socketAddr: {
address: string;
protocol: "tcp" | "udp";
}
} | {
pipe: {
mode: number;
path: string;
}
}
HttpRequest
Data and metadata of the HTTP request.
Property | Type | Description | Example |
---|---|---|---|
host | string | Request host name | |
path | string | API route | /api/path |
scheme | string | HTTP scheme | http , https |
method | string | HTTP method | GET , 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" } |
query | string | querystring | param1=value1 (no question mark; empty string if no querystring available) |
data | number[] | 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
:
- JavaScript
- TypeScript
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"];
Import class(s)
import {
// ...
HttpPayload,
} from "@fstnetwork/loc-logic-sdk";
const payload: { http: HttpPayload } = await ctx.payload();
const query = payload.http.request.query;
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?"
]
}
- JavaScript
- TypeScript
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) {
// ...
}
Import class(s) and define interface(s)
import {
// ...
HttpPayload,
} from "@fstnetwork/loc-logic-sdk";
interface Parsed {
name: string;
age: number;
job: {
title: string;
salary: number;
};
quotes: string[];
}
const payload: { http: HttpPayload } = await ctx.payload();
const data = payload.http?.request.data;
let parsed: Parsed = null;
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) {
// ...
}