REST Services

Representational State Transfer
Rest is an architectural style
It's not a standard itself, but does use standards such as HTTP, URL, XML, etc.


RestSharp

This is an open source library designed to make consuming APIs from .NET easier.
RestSharp makes it easy by abstracting away some of the complication of dealing with raw HTTP requests and providing a developer-friendly way of getting at what's important: the data.


Crafting an HTTP request can be done in several ways, with the most common way being the use of System.Net.Http.HttpClient or the simpler System.Net.WebClient.
System.Net.HttpWebRequest is not recommended unless you're doing something specific that requires lower-level access.


HttpClient is the newest one, requiring the Microsoft .NET Framework 4.5 and exposing only asynchronous methods for sending requests.
It's the most similar to RestSharp in that it's easy to use (not overly abstract and not overly complicated either), but because it requires the .NET Framework 4.5, not every application can take advantage of it.


WebClient is very simple and a great choice for just making GET requests, but once you need to start using verbs like PUT or DELETE the code starts to get quite a bit messier.



namespace RestSharp 

Calling REST


Download .NET 6.0 Runtime
Open VS Code
Check the dotnet version

dotnet --info 

Install the C# Extension


Create folder

cd c:\temp\vscode\consoleapp1 
dotnet new console
code -a.

dotnet run 

Microsoft Graph



GitHub Repos

Open Visual Studio 2022
New Project, C#, Windows, Console, Console App
Change the Name to "Console_Services_REST"
Change the Location to somewhere on your c drive and press Next
Check the .NET version is correct and press Create.
The solution contains one project and that project contains a single source file called program.cs
SS


REST Service JSON

We are going to use the following REST service:

https://api.github.com/users/officeaddins/repos 

Copy this into a browser and check that it works and returns JSON.


namespace consoleapp1 
{
  class Program
  {
    private static readonly
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

    private static async
    System.Threading.Tasks.Task
    Main(string[] args)
    {
      var repositories = await Method_ProcessRepositories();

      foreach (Class_Repository repo in repositories)
      {
        System.Console.WriteLine(repo.Property_Name);
        System.Console.WriteLine(repo.Property_Description);
        System.Console.WriteLine(repo.Property_HTMLUrl);
        System.Console.WriteLine();
      }
    }

    private static async
    System.Threading.Tasks.Task<System.Collections.Generic.List<Class_Repository>>
    Method_ProcessRepositories()
    {
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(
          new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
      client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

      var streamTask = client.GetStreamAsync("https://api.github.com/users/officeaddins/repos");

      var repositories =
      await System.Text.Json.JsonSerializer.DeserializeAsync<System.Collections.Generic.List<Class_Repository>>(await streamTask);

      if (repositories is not null)
      {
        return repositories;
      }
      else
      {
        return new System.Collections.Generic.List<Class_Repository>();
      }
    }
  }

  public class 
  Class_Repository
  {
    [System.Text.Json.Serialization.JsonPropertyName("name")]
    public string? Property_Name { get; set; }

    [System.Text.Json.Serialization.JsonPropertyName("description")]
    public string? Property_Description { get; set; }

    [System.Text.Json.Serialization.JsonPropertyName("html_url")]
    public System.Uri? Property_HTMLUrl { get; set; }

  }
}


public class Class1 
    {
        private const string URL = "https://sub.domain.com/objects.json?api_key=123";
        private const string DATA = @"{""object"":{""name"":""Name""}}";

        static void Main(string[] args)
        {
            Class1.CreateObject();
        }

        private static void CreateObject()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = DATA.Length;
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
            requestWriter.Write(DATA);
            requestWriter.Close();

             try {
                WebResponse webResponse = request.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                Console.Out.WriteLine(response);
                responseReader.Close();
            } catch (Exception e) {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }

        }
    }



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext