Http Client in UnrealScript

Here’s an idiomatic UnrealScript example demonstrating the concept of an HTTP client:

class HTTPClientExample extends Object;

var HttpRequest Request;
var HttpResponse Response;

function Init()
{
    // Create a new HTTP request
    Request = class'HttpRequest'.static.CreateRequest();
    
    // Set the URL for the request
    Request.SetURL("https://example.com");
    
    // Set the request method to GET
    Request.SetVerb("GET");
    
    // Add a completion delegate
    Request.OnProcessRequestComplete = OnRequestComplete;
    
    // Send the request
    Request.ProcessRequest();
}

function OnRequestComplete(HttpRequest InRequest, HttpResponse InResponse, bool bWasSuccessful)
{
    local string ResponseBody;
    local array<string> ResponseLines;
    local int i;
    
    if (bWasSuccessful)
    {
        // Print the HTTP response status
        `log("Response status:" @ InResponse.GetResponseCode());
        
        // Get the response body
        ResponseBody = InResponse.GetContentAsString();
        
        // Split the response body into lines
        ParseStringIntoArray(ResponseBody, ResponseLines, "\n", true);
        
        // Print the first 5 lines of the response body
        for (i = 0; i < Min(5, ResponseLines.Length); i++)
        {
            `log(ResponseLines[i]);
        }
    }
    else
    {
        `log("Request failed");
    }
}

defaultproperties
{
}

This UnrealScript example demonstrates how to create an HTTP client and make a simple GET request. Here’s a breakdown of the code:

  1. We define a class HTTPClientExample that extends Object.

  2. We declare variables for HttpRequest and HttpResponse, which are UnrealScript classes for handling HTTP communication.

  3. The Init() function sets up and sends the HTTP request:

    • We create a new HttpRequest object.
    • We set the URL for the request to “https://example.com”.
    • We set the request method to GET.
    • We assign a completion delegate OnRequestComplete to handle the response.
    • Finally, we send the request using ProcessRequest().
  4. The OnRequestComplete() function is called when the request is completed:

    • It checks if the request was successful.
    • If successful, it prints the response status code.
    • It then retrieves the response body as a string.
    • The response body is split into lines.
    • The first 5 lines of the response (or fewer if there are less than 5) are printed to the log.
  5. We use UnrealScript’s backtick log syntax (`log) for logging, which is equivalent to Console.Log in UE3 or UE_LOG in UE4.

  6. The defaultproperties block is empty in this case but is included as it’s a common practice in UnrealScript.

To use this code in an Unreal Engine project:

  1. Create a new script file named HTTPClientExample.uc in your project’s Scripts folder.
  2. Copy the code into this file.
  3. Compile the script as part of your project.
  4. You can then create an instance of this class and call the Init() function to start the HTTP request.

Note that UnrealScript’s HTTP functionality may vary depending on the version of Unreal Engine you’re using. This example assumes a relatively recent version with built-in HTTP request capabilities. In older versions, you might need to use platform-specific or third-party plugins for HTTP communication.

Also, be aware that UnrealScript is primarily used in Unreal Engine 3 and earlier versions. For Unreal Engine 4 and later, you would typically use C++ or Blueprint for similar functionality.