Hi
How to create & consume API using Rest Sharp
Thanks
Hi
How to create & consume API using Rest Sharp
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 14, 2023, 12:02 PM EDT
Hello Ramco,
To create & consume API using Rest Sharp you need to do following.
1. You need to install RestSharp by adding it as a NuGet package to your .NET project. To do this, right-click on your project in Visual Studio and select "Manage NuGet Packages". Search for "RestSharp" and install the package.
2 Then you can consume API as show in below code:
using RestSharp; using Newtonsoft.Json; class Program { static void Main(string[] args) { // Step 1: Create a RestSharp client var client = new RestClient("https://jsonplaceholder.typicode.com"); // Step 2: Create a RestSharp request var request = new RestRequest("posts/{id}", Method.GET); request.AddUrlSegment("id", "1"); // Step 3: Send the RestSharp request var response = client.Execute(request); // Step 4: Process the RestSharp response if (response.IsSuccessful) { var data = JsonConvert.DeserializeObject(response.Content); Console.WriteLine($"Title: {data.title}\nBody: {data.body}"); } else { Console.WriteLine($"Error: {response.StatusCode} - {response.ErrorMessage}"); } Console.ReadKey(); } } public class Post { public int userId { get; set; } public int id { get; set; } public string title { get; set; } public string body { get; set; } }Here i have given only GET opration code. Please refer below link for other like POST, PUT etc..
https://code-maze.com/aspnetcore-using-restsharp-consume-api/
Thanks
Naimish Makwana
Sachin SinghRank #66 · 26.2K reputationPosted Mar 14, 2023, 12:28 PM EDT
please share your code if you have any query?