HTTP Agent - JS/TS
Send HTTP request to a HTTP endpoint and fetch the response.
Logic Type | Available |
---|---|
Generic logic | ✅ |
Aggregator logic | ❌ |
HTTP Agent Configuration
See: Agent Configuration
A HTTP Agent Configuration defines an external HTTP host that will be allowed to be accessed from LOC runtime.
The HTTP Agent requires a configuration reference name so that it can access external endpoints on the host. The reference name can be added to a logic while creating or editing a data process.
Import and Usage
- JavaScript
- TypeScript
import {
HttpAgent,
} from "@fstnetwork/loc-logic-sdk";
export async function run(ctx) {
const httpClient = await HttpAgent.acquire("http-config-ref");
const response = await httpClient?.fetch(path);
}
export async function handleError(ctx, error) {
// ... same
}
import {
HttpAgent,
GenericContext,
RailwayError,
} from "@fstnetwork/loc-logic-sdk";
export async function run(ctx: GenericContext) {
const httpClient = await HttpAgent.acquire("http-config-ref");
const response = await httpClient?.fetch(path);
}
export async function handleError(ctx: GenericContext, error: RailwayError) {
// ... same
}
Class Reference
Type
HttpAgent
Method: Acquire HTTP Client
async acquire(configurationName: string): Promise<HttpAgentClient>
Parameter | Description |
---|---|
configurationName | HTTP Agent Configuration reference name |
Acquire a HTTP client using a configuration reference name. Throws an error if the configuration cannot be found.
HTTP Client
Type
HttpAgentClient
Importable from
@fstnetwork/loc-logic-sdk
Method: Send HTTP Request
async fetch(input: Request | string, init?: RequestInit): Promise<Response>
Parameters | Description | Reference |
---|---|---|
input | Request resource | resource |
init? | Request options | options |
Fetch a HTTP endpoint. This method is almost identical to Fetch API.
Returns a Response
object.
The URL can only contain the path (without host name, which is already in the HTTP Agent Configuration).
The path can contain querystring.
Response
Similar to Fetch API's Response.
Properties:
Property | Type | Description |
---|---|---|
status | number | HTTP response status code |
statusText | string | HTTP response status message |
ok | boolean | If request was successful (HTTP status code 2xx ) |
type | ResponseType | Response type ("basic" , "cors" , "default" , "error" , "opaque" or "opaqueredirect" ) |
url | string | Response URL |
headers | Headers | Response headers |
body | ReadableStream<Uint8Array> | null | Response body |
Methods:
// convert body to plaintext
async text(): Promise<string>
// convert body to JSON object (may throw an error if conversion failed)
async json(): Promise<any>
After calling text()
or json()
, the body (a ReadableStream
object) will be consumed and locked from further access. See Using readable streams for additional info.
Examples
GET Request with Querystring
const queryString = "name=Arthur+Dent&age=42";
/* or:
const params = {
name: "Arthur Dent",
age: 42
};
const queryString = new URLSearchParams(params).toString();
*/
const httpClient = await HttpAgent.acquire("http-config-ref");
const response = await httpClient?.fetch(`/api/path?${queryString}`);
// if the response status is 2xx
if (response.ok) {
// ...
}
POST Request and Parse Response
const data = {
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 httpClient = await HttpAgent.acquire("http-config-ref");
const response = await httpClient?.fetch("/api/path", {
method: "POST",
headers: {
"Content-Type": "application/json; charset=UTF-8",
},
body: JSON.stringify(data), // convert body to string
});
// get HTTP response status code
const statusCode = response.status;
// get headers
const headers = Object.fromEntries(response.headers);
const authorization = headers["Authorization"];
const contentType = headers["Content-Type"];
// if the response status is 2xx
let body = null;
if (response.ok) {
// get body as string
body = await response.text();
// or get body as JSON object (the response stream object can only be read once):
// body = await response.json();
// ...
}