Skip to main content
The HTTP request functions allow your server scripts to communicate with external web services and APIs. These functions use libcurl internally and support GET, POST, PUT, PATCH, and DELETE methods.

GET Requests

httpGet

Performs an HTTP GET request and returns the server’s response.
response = httpGet(url, [headers...])
url
string
required
The full URL to request (e.g., "https://api.example.com/users")
headers
string
Optional HTTP headers (variadic). Each header should be a string in the format "Key: Value"
response
string | undefined
Returns the response body as a string, or undefined if the request fails
Example: Fetching data
response = httpGet("https://api.example.com/players");
if (isDefined(response)) {
    iPrintLn("Response: " + response);
}
Example: With custom headers
response = httpGet(
    "https://api.example.com/data",
    "Authorization: Bearer token123",
    "Accept: application/json"
);

POST Requests

httpPost

Performs an HTTP POST request with a request body.
response = httpPost(url, body, [headers...])
url
string
required
The full URL to request
body
string
required
The request body (typically JSON or form data)
headers
string
Optional HTTP headers (variadic)
response
string | undefined
Returns the response body as a string, or undefined if the request fails
If no Content-Type header is provided, it defaults to application/json.
Example: Sending JSON data
playerData = "{\"name\":\"" + player.name + "\",\"score\":" + player.score + "}";
response = httpPost("https://api.example.com/players", playerData);
Example: With authentication
response = httpPost(
    "https://api.example.com/submit",
    "{\"data\":\"value\"}",
    "Authorization: Bearer token123",
    "Content-Type: application/json"
);

PUT Requests

httpPut

Performs an HTTP PUT request to update a resource.
response = httpPut(url, body, [headers...])
url
string
required
The full URL to request
body
string
required
The request body
headers
string
Optional HTTP headers (variadic)
response
string | undefined
Returns the response body as a string, or undefined if the request fails
Example: Updating a resource
response = httpPut(
    "https://api.example.com/players/123",
    "{\"score\":1500}"
);

PATCH Requests

httpPatch

Performs an HTTP PATCH request to partially update a resource.
response = httpPatch(url, body, [headers...])
url
string
required
The full URL to request
body
string
required
The request body
headers
string
Optional HTTP headers (variadic)
response
string | undefined
Returns the response body as a string, or undefined if the request fails
Example: Partial update
response = httpPatch(
    "https://api.example.com/users/456",
    "{\"status\":\"online\"}"
);

DELETE Requests

httpDelete

Performs an HTTP DELETE request to remove a resource.
response = httpDelete(url, [body], [headers...])
url
string
required
The full URL to request
body
string
Optional request body
headers
string
Optional HTTP headers (variadic)
response
string | undefined
Returns the response body as a string, or undefined if the request fails
Example: Deleting a resource
response = httpDelete("https://api.example.com/sessions/789");

Generic HTTP Request

httpRequest

Performs an HTTP request with any custom method.
response = httpRequest(method, url, [body], [headers...])
method
string
required
The HTTP method (e.g., "GET", "POST", "HEAD", "OPTIONS")
url
string
required
The full URL to request
body
string
Optional request body
headers
string
Optional HTTP headers (variadic)
response
string | undefined
Returns the response body as a string, or undefined if the request fails
Example: Custom method
response = httpRequest("HEAD", "https://api.example.com/status");

Deprecated Function

httpPostRequest

This function is deprecated. Use httpPost() instead.
Legacy HTTP POST request function.
response = httpPostRequest(host, port, path, postData, [receive])
host
string
required
The hostname or IP address
port
int
required
The port number
path
string
required
The request path
postData
string
required
The POST data
receive
int
Whether to receive a response (1 = yes, 0 = no). Default: 1

Best Practices

Use HTTPS

Always use HTTPS URLs for secure communication. SSL verification is disabled by default.

Handle Errors

Always check if the response is undefined before using it.

Escape JSON

Properly escape quotes in JSON strings using backslashes.

Async Behavior

HTTP requests are synchronous and will block script execution. Keep requests fast.

Complete Example

Example: Player authentication system
authenticatePlayer(player) {
    // Prepare player data as JSON
    guid = player getGuid();
    name = player.name;
    
    jsonData = "{";
    jsonData += "\"guid\":\"" + guid + "\",";
    jsonData += "\"name\":\"" + name + "\",";
    jsonData += "\"timestamp\":" + getTime();
    jsonData += "}";
    
    // Send authentication request
    response = httpPost(
        "https://api.example.com/auth",
        jsonData,
        "Authorization: Bearer server_token_123",
        "Content-Type: application/json"
    );
    
    if (!isDefined(response)) {
        iPrintLn("Authentication failed: No response");
        return false;
    }
    
    // Parse response (simplified - use JSON parser in production)
    if (isSubStr(response, "\"authenticated\":true")) {
        iPrintLn(player.name + " authenticated successfully");
        return true;
    }
    
    return false;
}

reportServerStats() {
    players = getEntArray("player", "classname");
    
    stats = "{";
    stats += "\"server\":\"" + getCvar("sv_hostname") + "\",";
    stats += "\"players\":" + players.size + ",";
    stats += "\"map\":\"" + getDvar("mapname") + "\"";
    stats += "}";
    
    response = httpPost("https://stats.example.com/report", stats);
    
    if (isDefined(response)) {
        iPrintLn("Stats reported successfully");
    }
}

checkForUpdates() {
    response = httpGet(
        "https://updates.example.com/version",
        "User-Agent: CoD4-Unleashed-Server"
    );
    
    if (isDefined(response)) {
        latestVersion = response;
        currentVersion = "1.0.0";
        
        if (latestVersion != currentVersion) {
            iPrintLnBold("Server update available: " + latestVersion);
        }
    }
}

Build docs developers (and LLMs) love