HttpRequest

 

The HttpRequest function sends an HTTP request to a specified URL and returns a response.

 

Note) Due to the delay in responding to the other server, the watchdog may remain in a standby state for up to 30 seconds.

To avoid this, write a script that executes the HttpRequest function when the request flag is turned on by using the "Use Thread" feature of the script at program running.

 

string @HttpRequest(string method, string url, string data, string headerId);

 

Parameters

string method : HTTP Method (GET, POST, PUT, PATCH, DELETE)

string url : Request URL

string data : Request body data ( Ignored in GET, DELETE)

string headerId : Identifier of the header collection to use

 

Return Value

Return a response body on success.

Returning an empty string on failure.

 

Example 1)

// GET Request Example

string method = "GET";

string url = "https://api.example.com/data";

string data = ""; // Ignored in GET

string headerId = "myHeader";

string response = @HttpRequest(method, url, data, headerId);

 

Example 2)

// POST Request Example

method = "POST";

url = "https://api.example.com/users";

data = @JsonToString(buf);   // buf example value: "{\"name\":\"John Doe\",\"email\":\"hong@example.com\"}"; 

response = @HttpRequest(method, url, data, headerId);

 

Example 3)

// PUT Request Example

method = "PUT";

url = "https://api.example.com/users/123";

data = @JsonToString(buf);   // buf example value:"{\"name\":\"John Doe\"}";

response = @HttpRequest(method, url, data, headerId);

 

Example 4 (Integration usage examples)

// Generate headers

string headerId = "apiHeader";

@HttpCreateHeader(headerId);

 

// Add Header list

@HttpAddHeader(headerId, "Content-Type", "application/json");

@HttpAddHeader(headerId, "Accept", "application/json");

@HttpAddHeader(headerId, "User-Agent", "AutoBase/1.0");

 

// Or load the configuration from the header file

@HttpLoadHeaderConfig(headerId, "common_api");

 

// Perform API requests

string data = @JsonToString(buf);   // buf example value: "{\"query\":\"User Data\"}"

string response = @HttpRequest("POST", "https://api.example.com/data", data , headerId);

if( response != "") @Message("Successfully received data.");

else @Message("Successfully received data. The request failed.);

 

// Remove headers after use

@HttpRemoveHeader(headerId);

 

Example 5)

@HttpCreateHeader("header1");

@HttpLoadHeaderConfig("header1","api1");

data = @JsonTemplateLoad("test");

url = "http://192.168.1.2:5000/obj";

buf = @HttpRequest("POST",url, data, "header1");

@ObjectSetText("Text1", buf);

 

Example 6 (Naver API Shopping Search)

@HttpCreateHeader("header1");
@HttpAddHeader("header1","X-Naver-Client-Id","TestClientId12345");         //Enter NAVER API ID
@HttpAddHeader("header1","X-Naver-Client-Secret","TestClientSecret");      //Enter Naver API password

url = "https://openapi.naver.com/v1/search/shop.json?query=Autobase&display=1&start=1&sort=sim";  //Search Autobase
responseString = @HttpRequest("GET", url, "", "header1");        //httpMethod, url, data, header
@ObjectSetText("Text1", responseString);   //Full Response

buf1 = @StringJson(responseString, "$.items[0].link");     //Extract only the first product link.
@ObjectSetText("Text2", buf1);

 

Version information

Supported Version: 10.3.6.25 or later

 

Related helps)

HttpCreateHeader

HttpAddHeader

HttpRemoveHeader

HttpLoadHeaderConfig

HttpRequest