RestSharp

RestSharp is a lightweight and easy-to-use HTTP client for .NET, designed to simplify sending HTTP requests and working with RESTful web services. If you prefer using RestSharp over the native .NET HttpClient, NBomber provides NBomber.RestSharp plugin to work with it.
You can find the source code here.
To install NBomber.RestSharp package you should execute the following dotnet command:
dotnet add package NBomber.RestSharp
RestSharp API
Here is a basic example of using the RestSharp client with NBomber.
var options = new RestClientOptions($"http://localhost:60529");
var client = RestClientBuilder.CreateDefaultClient(options);
var request = new RestRequest("api/users/{id}");
request.AddParameter("id", userId, ParameterType.UrlSegment);
// PUT
var response = await client.SendPut(request);
// GET
var response = await client.SendGet<User>(request);
You can find the complete example by this link.
CreateDefaultClient
This method creates a default preconfigured instance of RestSharp client.
RestClient CreateDefaultClient(int maxConnectionsPerServer = 5000)
We highly recommend using CreateDefaultClient
instead of using the default constructor new RestClient()
. This is because CreateDefaultClient
applies important preconfigured settings to the underlying SocketsHttpHandler, including:
- MaxConnectionsPerServer: 5000
- Timeout: 1 minute
- PooledConnectionLifetime: 10 minutes
- PooledConnectionIdleTimeout: 5 minutes
These settings enhance connection management and ensure more efficient use of network resources.
Example:
var options = new RestClientOptions("https://nbomber.com");
var client = RestClientBuilder.CreateDefaultClient(options);
Send
To send requests, receive responses, and measure payload size, NBomber.RestSharp offers a set of methods: SendGet, SendPost, SendPut, SendPatch, and SendDelete
. All NBomber methods follow a consistent naming pattern: Send{HTTP_Method_Name}
.
// GET
var request = new RestRequest("/api/pingpong/");
var response = await client.SendGet(request);
// POST wiht JSON body
var request = new RestRequest("/api/pingpong/");
request.AddJsonBody(new User("name", "last_name", 42));
var response = await client.SendPost(request);
In addition to the specialized Send{HTTP_Method_Name}
methods, NBomber also provides a generic Send
method for sending any custom request.
// HEAD
var request = new RestRequest("/api/pingpong/");
request.Method = Method.Head;
var response = await client.Send(request);
// or Send<T>
// var response = await client.Send<T>(request);