RestSharp In .NET 6.0

Introduction

The Rest sharp V107 is the major update, Before this version the calling of the Rest API in C# is different, but in this version, there are a lot of changes, 

In this article, we saw what is the major changes and how to call the Rest sharp API in Asp.net Core.

Let's Start 

GET API

Previous version Of Restsharp API 

var client = new RestClient("https://reqres.in/api/users?page=2");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Now Comes in Version V107 with .Net 6.0

public async Task < IActionResult > IndexAsync() {
    var url = "https://reqres.in/api/users?page=2";
    var client = new RestClient(url);
    var request = new RestRequest(url, Method.Get);
    RestResponse response = await client.ExecuteAsync(request);
    var Output = response.Content;
    return View();
}

POST API

Previous version Of Restsharp API 

var client = new RestClient("https://reqres.in/api/users");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
var body = @ "{
" + "\
n " +
@ "    "
"name"
": "
"Ajay Kumar"
",
" + "\
n " +
@ "    "
"job"
": "
"Developer"
"
" + "\
n " +
@ "}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Now Using Version V107 With .net6.0

public async Task < IActionResult > IndexAsync() {
    var url = "https://reqres.in/api/users";
    var client = new RestClient(url);
    var request = new RestRequest(url, Method.Post);
    request.AddHeader("Content-Type", "application/json");
    var body = new {
        name = "Ajay Kumar",
            job = "Developer"
    };
    var bodyy = JsonConvert.SerializeObject(body);
    request.AddBody(bodyy, "application/json");
    RestResponse response = await client.ExecuteAsync(request);
    var output = response.Content;
    return View();
}

PUT API 

Previous version Of Restsharp API

var client = new RestClient("https://reqres.in/api/users/2");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
var body = @ "{
" + "\
n " +
@ "    "
"name"
": "
"Ajay"
",
" + "\
n " +
@ "    "
"job"
": "
"Developer"
"
" + "\
n " +
@ "}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Now Using Version V107 With .net6.0

public async Task < IActionResult > IndexAsync() {
    var url = "https://reqres.in/api/users";
    var client = new RestClient(url);
    var request = new RestRequest(url, Method.Put);
    request.AddHeader("Content-Type", "application/json");
    var body = new {
        name = "Ajay",
            job = "Developer"
    };
    var bodyy = JsonConvert.SerializeObject(body);
    request.AddBody(bodyy, "application/json");
    RestResponse response = await client.ExecuteAsync(request);
    var output = response.Content;
    return View();
}

Delete API

Previous version Of Restsharp API

var client = new RestClient("https://reqres.in/api/users/2");
client.Timeout = -1;
var request = new RestRequest(Method.DELETE);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Now Using Version V107 With .net6.0

public async Task < IActionResult > IndexAsync() {
    var url = "https://reqres.in/api/users?page=2";
    var client = new RestClient(url);
    var request = new RestRequest(url, Method.Delete);
    RestResponse response = await client.ExecuteAsync(request);
    var output = response.Content;
    return View();
}

Conclusion

You can see in the above example there are lots of changes, the method of calling the API are total changes, if you want to check more details you check it out the below URL