This is the code I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Net.Http;
usi
Here are a few different ways of calling an external API in C# (updated 2019).
.NET's built-in ways:
Free, open-source NuGet Packages, which frankly have a much better developer experience than .NET's built in clients:
All the above packages provide a great developer experience (i.e., concise, easy API) and are well maintained.
(*) as at August 2019
Example: Getting a Todo item from a Fake Rest API using ServiceStack.Text. The other libraries have very similar syntax.
class Program
{
static void Main(string[] args)
{
// Fake rest API
string url = "https://jsonplaceholder.typicode.com/todos/1";
// GET data from API & map to POCO
var todo = url.GetJsonFromUrl().FromJson();
// Print the result to screen
todo.PrintDump();
}
public class Todo
{
public int UserId { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public bool Completed { get; set; }
}
}
Running the above example in a .NET Core Console app, produces the following output.
Install these packages using NuGet
Install-Package ServiceStack.Text, or
Install-Package RestSharp, or
Install-Package Flurl.Http