Skip to content

informat.http HTTP Requests ​

Overview ​

Use the informat.http object to make HTTP network requests. After calling the unified informat.http.request method, obtain the response object.

INFO

  • Since network fluctuations or service exceptions on the called side may cause request failures, such as common 404, 502, etc. HTTP status codes. Therefore, please get the corresponding status code after calling to handle the business logic.
  • Some services may redirect to another URL after being called. In such cases, you can set followRedirect to true in the request object to automatically redirect to the actual URL, or handle it manually in your business logic.

request ​

Send an HTTP request

javascript
informat.http.request(request);
ParameterTypeDescription
requestHttpRequestRequest object

Return Value Type HttpResponse Returns the content of the HTTP request

Example

js
const req = {
  url: "https://demo.api.com/api",
  method: "POST",
  timeout: 1000,
  form: {
    p1: 1,
    p2: "str",
  },
};
informat.http.request(req);

Return Value Type HttpResponse Returns the content of the HTTP request

Response ​

statusCode ​

HTTP status code

js
response.statusCode();

Return Value

Type Integer

body ​

Response content

js
response.body();

Return Value

Type String

headers ​

Get HTTP request headers

js
response.headers();

Return Value

Type Object<String,String>

contentEncoding ​

Response content encoding

js
response.contentEncoding();

Return Value

Type String

contentLength ​

Response content length

js
response.contentLength();

Return Value

Type Integer

saveBodyAsFile ​

Save file to local storage

js
response.saveBodyAsFile(path);
ParameterTypeDescription
pathStringStorage path

saveBodyAsStorage ​

Save file to file shared storage

js
response.saveBodyAsStorage(path);
ParameterTypeDescription
pathStringStorage path

saveBodyAsAttachment ​

Save response content as attachment object

js
response.saveBodyAsAttachment(tableKey, fieldKey, fileName);
ParameterTypeDescription
tableKeyStringTable key
fieldKeyStringField key
fileNameStringFile name

Return Value

Type Attachment

Examples ​

Example 1: POST request example ​

js
const req = {
  url: "https://demo.api.com/api",
  method: "POST",
  timeout: 1000,
  form: {
    p1: 1,
    p2: "str",
  },
};
const rsp = informat.http.request(req);
console.log(rsp.body()); //Request result

Example 2: GET request example ​

javascript
const req = {
  url: "https://demo.api.com/api?a=zhangsan&b=lisi",
  method: "GET",
  timeout: 1000,
};
const ctx = informat.http.request(req);
console.log(ctx.body(), "...."); //Request result
javascript
const req = {
  url: "https://demo.api.com/api",
  method: "GET",
  timeout: 1000,
  form: {
    a: "zhangsan",
    b: "lisi",
  },
};
const ctx = informat.http.request(req);
console.log(ctx.body(), "...."); //Request result

Example 3: Download remote file and store it in local sandbox environment ​

js
const req = {
  url: "https://ai.ainformat.com//logo.png",
  followRedirect: true,
  maxRedirectCount: 10,
};
const rsp = informat.http.request(req);
rsp.saveBodyAsFile("logo.png");

Example 4: Download remote file and store it in s3 shared storage ​

js
const req = {
  url: "https://ai.ainformat.com//logo.png",
  followRedirect: true,
  maxRedirectCount: 10,
};
const rsp = informat.http.request(req);
rsp.saveBodyAsStorage("logo.png");

Example 5: Call request and upload file to file shared storage and save it as attachment object ​

js
let req = {
  url: "https://demo.api.com/api",
  followRedirect: true,
  maxRedirectCount: 10,
};
let rsp = informat.http.request(req);
const attachment = rsp.saveBodyAsAttachment("tableKey", "fieldKey", "demo.png");
// After saving as an attachment, you can perform operations like updating data, inserting data, etc. (tableKey and fieldKey need to be consistent)
const insertBean = {
  name: "demo name",
  attachment: attachment, // If it's multiple selection, wrap it with [] like: attachment: [attachment]
};
// Call the new data interface
informat.table.insert("tableKey", insertBean);

Example 6: Upload file from local sandbox environment to third-party interface ​

js
const req = {
  url: "https://demo.api.com/api",
  method: "POST",
  timeout: 5000,
  files: {
    file: "demo.jpg",
  },
};
const resp = informat.http.request(req);
console.log(resp.statusCode());
console.log(resp.body());

Example 7: Upload file from file shared storage to third-party interface ​

js
const req = {
  url: "https://demo.api.com/api",
  method: "POST",
  timeout: 5000,
  storages: {
    file: "demo.jpg",
  },
};
const resp = informat.http.request(req);
console.log(resp.statusCode());
console.log(resp.body());