Send HTTP POST request asynchronously. The function immediately returns the task key without blocking the script execution. 


Asynchronous HTTP functions don't return the server's response immediately. They run the request in a separate thread and return a string key, which can be used to retrieve the ready result from the server's internal memory cache via MemCacheExists() and MemCacheGet() later. 


Syntax

function HTTPSendPostMessageAsync(const AURL, ABody, AHeaders: string; const ATimeOut: integer): string;

 

Parameters and return values

Parameter

Type

Value

AURL

string

address, where you send a request. Supports both http and https. You can specify any port for connection, for example, https://yourserver.com:8080/myservice/

ABody

string

body of a POST request;

AHeaders

string

special headers (if necessary). Separated by CRLF if required more than one. Usually, if the service does not require this — an empty string;

ATimeOut

integer

time in milliseconds, maximum time to wait for response from the remote server.

 

Function result

String key that allows getting result via MemCacheGet(). If the request has not yet completed, the corresponding cache entry may not exist.

 

Example


var
  sKey, sBody, sResult, sData, sReqID: string;
  i: integer;
begin
  sReqID := CreateGUIDString;
  sBody := '{}';
  JSONSetString(sBody, 'event', 'user_login');
  JSONSetString(sBody, 'request_id', sReqID);
  sKey := HTTPSendPostMessageAsync(
    'https://httpbin.org/post',
    sBody,
    'Content-Type: application/json' + CRLF +
    'Accept: application/json',
    3000);
  mLogScript(sKey, 'Key');
// wait for the result to appear in the cache, maximum 3 seconds.
// example code. Similar loops with ScriptSleep in production is a bad idea.
  for i := 1 to 30 do begin
    if MemCacheExists(sKey) then begin
      sResult := MemCacheGet(sKey);
      mLogScript(sResult, 'Result'); 
      if JSONValid(sResult) then begin
        if JSONGetString(sResult, 'data', sData) = 0 then
          mLogScript('POST body accepted: ' + sData, 'HTTP POST ASYNC');
      end
      else
        mLogScript('Invalid JSON: ' + sResult, 'HTTP POST ASYNC');
      break;
    end;
    ScriptSleep(100);
  end;
end.


Script work result

[19:28:24] (Log "PostMessageAsync"): [Key] httppost_async{3f996968-6227-4cfe-a2b9-ffebe65a3644}21032026192824156

[19:28:24] (Log "PostMessageAsync"): [Result] { "args": {}, "data": "{\"request_id\":\"{698B5999-CCF1-4419-81A2-2C455FF1467F}\",\"event\":\"user_login\"}", "files": {}, "form": {}, "headers": { "Accept": "application/json", "Content-Length": "76", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "Mozilla/4.0 (compatible; Synapse)", "X-Amzn-Trace-Id": "Root=1-69bed538-18d2c272565edcc45622c527" }, "json": { "event": "user_login", "request_id": "{698B5999-CCF1-4419-81A2-2C455FF1467F}" }, "origin": "95.67.34.140", "url": "https://httpbin.org/post" }

[19:28:24] (Log "PostMessageAsync"): [HTTP POST ASYNC] POST body accepted: {"request_id":"{698B5999-CCF1-4419-81A2-2C455FF1467F}","event":"user_login"}

[19:28:24] (Run "PostMessageAsync"): Script operation time: 576 мс

[19:28:24] (Run "PostMessageAsync"): Script done successfully.

 

See also

CreateGUIDString

CRLF

JSONGetString

JSONSetString

JSONValid

MemCacheExists

MemCacheGet

mLogScript

ScriptSleep