Skip to content

HTTP Request Examples ​

The examples which follow can easily be replicated. The details on the associated HTTP Connection has been included.

Please also review HTTP Response Examples.

A Simple GET Request Example ​

The following, is an example of a simple piece of HTTP scripting as a Custom Script in an Action, where the daily exchange rates are pulled from NZ Customs.

This script uses an HTTP Connection called NZ Customs Rates, which contains the following. Note that the Protocol is HTTPS.

image-20240408-001247.png

The SSL Settings contains the following. The Protocol has been set to Auto.

image-20240408-001522.png

Within the Custom Script, notice the use of a Try…Except around all of the HTTP coding, to capture any possible errors.

pascal
procedure ScriptEvent (var Value : variant);
var urlPath, q, q2, response_data: string;

begin
  q := '44 high';
  q2 := StringReplaceAll(q, ' ', '%20');
  urlPath := 'parceladdress/2.0/domestic/addresses?count=8&q=' + q2;
  LogInfo(urlPath);

 try
    HTTP.Connection := 'NZ Customs Rates';
    HTTP.Open('GET','');

    HTTP.Send;

 if HTTP.Response.ResponseCode <> 200 then //Response-Code 200 means OK
 begin
      LogError('Error - Response Code: ' + HTTP.Response.ResponseText);
      Exit;
 end;

    Setlength(response_data, HTTP.response.content.size);
    HTTP.response.content.read(response_data, HTTP.response.content.size);
    LogInfo('HTTP response size: ' + IntToStr(HTTP.response.content.size));

 except
    LogError('NZ Post API error: ' + ExceptionToString(ExceptionType, ExceptionParam));
 end;

end;

This script produced and sent the following successful HTTP GET Request.

image-20240325-225035.png

The token number has been intentionally blurred out

Please refer to Anatomy Of An HTTP Request for more detailed information.

This appears in the Log as follows. Notice the HTTP Trace tab.

image-20240327-213159.png

A Simple POST Request Example ​

The following, is an example of HTTP scripting as a Custom Script in an Action, where a simple JSON payload is sent to a host server using a POST.

This script uses an HTTP Connection called JSON Sample HTTP, which contains the following. Note that the Protocol is HTTPS.

image-20240408-224531.png

The SSL Settings contains the following. The Protocol has been set to Auto.

image-20240408-224642.png

There are also Headers, entered via the Headers tab.

image-20240408-224818.png

Within the Custom Script, notice the use of a Try…Except around all of the HTTP coding, to capture any possible errors. The payload is captured through the variable vHTTPRequestBodyStr.

pascal
procedure ScriptEvent (var Value : variant);
var
  vErrStr, vHTTPRequestBodyStr, vHTTPResponseBodyStr: string;
  vRequestBodyJSON : TJSONObject;
  vResponseBodySize: integer;
  vResponseBodyJSON: TJSONValue;
begin
  vErrStr := '';
  LogInfo('');                            // blank line
  vHTTPRequestBodyStr := '{"Greeting":"Hello there world! It is I. Horatio !"}';

 // Now to identify the HTTP Connection, and POST to the host ....
  HTTP.Connection := 'JSON Sample HTTP';
  HTTP.Open('POST', '');
  HTTP.Header['Content-Type'] := 'application/json';

  LogInfo('');
  LogInfo('The payload follows -'+vHTTPRequestBodyStr);
  LogInfo('');

  HTTP.ContentAsString := vHTTPRequestBodyStr;
  vHTTPResponseBodyStr := '';

 try
    HTTP.Send;
 except
    vErrStr := 'HTTP call failed with message ';
    vErrStr := vErrStr + '"' + ExceptionToString(ExceptionType, ExceptionParam) + '".';
 end;

 if (vErrStr = '') then
 begin
 // Extract and display the size of the payload
      vResponseBodySize := HTTP.Response.Content.Size;
      LogInfo('');
      loginfo('Message Body (payload) size = ' + inttostr(vResponseBodySize));
      vHTTPResponseBodyStr := HTTP.Response.ContentAsString;
 end;

  LogInfo('');
  LogInfo('Response:' + vHTTPResponseBodyStr);               // DIsplays the content of the response payload
  LogInfo('');

 if (vErrStr = '') then
 if (HTTP.Response.ResponseCode <> 201) then // Status code 201 means CREATED
 begin
      vErrStr := 'Internal error: HTTP call failed with ';
      vErrStr := vErrStr + 'Response code ' + IntToStr(HTTP.Response.ResponseCode) + ' ';
      vErrStr := vErrStr + 'and body:"';
      vErrStr := vErrStr + vHTTPResponseBodyStr + '".';
 end;

 if (vErrStr = '') then
 begin
      vResponseBodyJSON := TJSONValue.Parse(vHTTPResponseBodyStr);
 if (not Assigned(vResponseBodyJSON)) then
        vErrStr := 'HTTP Response Body failed to convert to JSON.';
 end;

 if (vErrStr = '') then
 if (not vResponseBodyJSON.IsObject) then
      vErrStr := 'HTTP Response Body is JSON but not a JSON Object.';

 if (vErrStr = '') then
 // no error ...
 else
 begin
    LogInfo('Err:' + vErrStr);
 end;

 if (Assigned(vRequestBodyJSON)) then
    vRequestBodyJSON.Free;

 if (Assigned(vResponseBodyJSON)) then
    vResponseBodyJSON.Free;

  SetLength(vHTTPResponseBodyStr, 0);
end;

This appears in the Log as follows. Notice the HTTP Trace tab.

image-20240408-230804.png

Found under the HTTP Trace tab, this script produced and sent the following successful HTTP POST Request.

image-20240408-234314.png