menu_bookDocumentation
HTTP Requests - s&box Networking Documentation
HTTP Requests
s&box provides an HTTP client for making web requests to external APIs. This is useful for fetching data, submitting scores to custom backends, or integrating with web services.
Allowed URLs
HTTP requests are restricted to allowed URLs for security. Allowed URLs are configured in your project's settings on sbox.game.
Cheat Sheet
Common HTTP operations:
CSHARP
// GET request that returns the response as a string
string response = await Http.RequestStringAsync( "https://api.example.com/data" );
// POST request with JSON content
await Http.RequestAsync(
"https://api.example.com/submit",
"POST",
Http.CreateJsonContent( new { score = 1000, player = "John" } )
);
// GET with JSON response
var data = await Http.RequestJsonAsync<MyDataType>( "https://api.example.com/data" );
// POST and get JSON response
var result = await Http.RequestJsonAsync<ResultType>(
"https://api.example.com/calculate",
"POST",
Http.CreateJsonContent( inputData )
);Request Options
CSHARP
// Add custom headers
var request = new HttpRequestMessage( HttpMethod.Get, "https://api.example.com/data" );
request.Headers.Add( "Authorization", "Bearer token123" );
request.Headers.Add( "X-Custom-Header", "value" );
var response = await Http.SendAsync( request );Response Handling
CSHARP
var response = await Http.RequestAsync( "https://api.example.com/data" );
// Check status code
if ( response.StatusCode == HttpStatusCode.OK )
{
// Read response body
string body = await response.Content.ReadAsStringAsync();
// Parse JSON
var data = JsonSerializer.Deserialize<MyData>( body );
}
else
{
Log.Error( $"HTTP Error: {response.StatusCode}" );
}Timeout and Cancellation
CSHARP
using var cts = new CancellationTokenSource();
cts.CancelAfter( TimeSpan.FromSeconds( 5 ) ); // 5 second timeout
try
{
var response = await Http.RequestAsync(
"https://api.example.com/data",
cancellationToken: cts.Token
);
}
catch ( OperationCanceledException )
{
Log.Warning( "Request timed out" );
}Rate Limiting
Be respectful of external APIs. Use delays between requests:
CSHARP
await Task.Delay( 100 ); // 100ms delay between requests
Was this helpful?