In .Net Core - NuGet:
Install-Package Rest.Service
For example in c# console:
using RestSharp.Service;
internal class Program
{
private IRestService restService = new RestService();
void Main(string[] args)
{
var response = restService.Request("https://google.com");
Console.WriteLine(response.StatusCode);
}
}
Default request is GET, also you can request on other actions (Post, Put, Delete, ...) e.g.
var response = restService.Request("https://google.com", HttpMethod.Post, new()
{
Authorization = new RestBearerAuth("JWT_TOKEN"),
Body = object,
Headers = new Dictionary<string, string>(),
Parameters = new Dictionary<string, string>()
});
Supported authentications:
RestBearerAuth("JWT_TOKEN")
RestBasicAuth(username, password)
RestHawkAuth(string authId, string authKey, HawkAlgorithm algorithm = HawkAlgorithm.Sha256)
1- At first, we need config Rest.Service in startup.cs
:
using RestSharp.Service;
Services.AddRestServices();
2- Now to use, do the following:
using RestSharp;
using RestSharp.Service.Interfaces;
public class HomeController : ControllerBase
{
private readonly IRestService restService;
public HomeController(IRestService _restService)
{
restService = _restService;
}
[HttpGet]
public IActionResult Index()
{
var response = restService.Request("https://google.com"); // GET Request
}
}