terminalCode Example
HTTP Requests
s&box provides the static Http class for asynchronous HTTP requests.
Allowed URLs
- Domains only (no IP addresses)
- localhost permitted on ports: 80, 443, 8080, 8443
- Use -allowlocalhttp command line flag for any local URL
Quick Examples
CSHARP
// GET request - return as string
string response = await Http.RequestStringAsync("https://api.example.com/data");
// POST JSON content
await Http.RequestAsync(
"https://api.example.com/player",
"POST",
Http.CreateJsonContent(playerData)
);GET with JSON Response
CSHARP
public class PlayerData
{
public string Name { get; set; }
public int Score { get; set; }
}
async Task FetchPlayerData(string playerId)
{
try
{
var response = await Http.RequestJsonAsync<PlayerData>(
$"https://api.example.com/players/{playerId}"
);
Log.Info($"Player: {response.Name}, Score: {response.Score}");
}
catch (Exception ex)
{
Log.Error($"Failed to fetch: {ex.Message}");
}
}POST with JSON
CSHARP
async Task SavePlayerData(PlayerData data)
{
var json = Json.Serialize(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await Http.RequestAsync(
"https://api.example.com/players",
"POST",
content
);
if (response.StatusCode == 200)
{
Log.Info("Player saved!");
}
}Request Headers
CSHARP
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com");
request.Headers.Add("Authorization", $"Bearer {token}");
request.Headers.Add("X-Game-Version", "1.0.0");
var response = await Http.SendAsync(request);Timeout Handling
CSHARP
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
try
{
var response = await Http.RequestStringAsync(url, cts.Token);
}
catch (OperationCanceledException)
{
Log.Warning("Request timed out");
}
Was this helpful?