codeAPI Reference
Sandbox.Http
Sandbox.Http
HTTP request utilities.
Overview
Http provides asynchronous methods for making HTTP requests to external services.
GET Request
CSHARP
// Simple GET
string response = await Http.RequestStringAsync("https://api.example.com/data");
// With headers
string response = await Http.RequestStringAsync("https://api.example.com/data",
HttpMethod.Get,
new Dictionary<string, string> { ["Authorization"] = $"Bearer {token}" });POST Request
CSHARP
// POST JSON
string json = "{\"name\":\"test\"}";
string response = await Http.RequestStringAsync("https://api.example.com/data",
HttpMethod.Post,
null,
json);JSON Requests
CSHARP
// GET and parse JSON
var data = await Http.RequestJsonAsync<MyResponse>("https://api.example.com/data");
// POST object as JSON
var result = await Http.RequestJsonAsync<ResponseType>("https://api.example.com/submit",
HttpMethod.Post,
null,
requestObject);Allowed URLs
s&box restricts HTTP to allowed domains. Configure in project settings:
- Domains must be explicitly allowed
- Localhost for development
- Specific domains for production
Error Handling
CSHARP
try
{
string response = await Http.RequestStringAsync(url);
}
catch (HttpRequestException ex)
{
Log.Error($"HTTP request failed: {ex.Message}");
}Timeout
CSHARP
// Default timeout is 30 seconds
// Use cancellation token for custom timeout
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
string response = await Http.RequestStringAsync(url, cancellationToken: cts.Token);Rate Limiting
CSHARP
// Implement rate limiting to avoid being blocked
RealTimeSince lastRequest = 100;
async Task RateLimitedRequest()
{
if (lastRequest < 1.0f) // 1 second between requests
{
await Task.Delay(1000);
}
var response = await Http.RequestStringAsync(url);
lastRequest = 0;
}Common Pattern
CSHARP
public class LeaderboardAPI
{
public async Task<Score[]> GetScores()
{
try
{
var token = await Auth.GetTokenAsync();
var scores = await Http.RequestJsonAsync<Score[]>(
"https://api.sbox.game/leaderboards/mygame/highscore",
HttpMethod.Get,
new Dictionary<string, string> { ["Authorization"] = $"Bearer {token}" });
return scores;
}
catch (Exception ex)
{
Log.Error($"Failed to get scores: {ex.Message}");
return Array.Empty<Score>();
}
}
}
Was this helpful?