Call Contentstack HTTPS JSON APIs from ASP.NET Blazor Clients

This blog post provides pointers for calling HTTP APIs that return JSON from ASP.NET Blazor client applications, specifically using the Contentstack SaaS headless content management system. If I understand correctly, with Blazor, developers can write C# code that runs as WebAssembly in the browser.

Update 20.April.2021: For a more comprehensive explanation and more robust code, see:

I do not understand the details, but my first challenge was that the Blazor client implementation seems to lack support for the HttpWebRequest and HttpWebResponse classes that the Contentstack .NET SDK and my custom code use to invoke HTTPS APIs. Apparently, HttpClient is supported. I hacked up a little hard-coded class to abstract some of the details.

namespace blazor
{
    using System;
    using System.Net.Http;

    public class ContentstackHttpClient : HttpClient
    {
        public ContentstackHttpClient()
        {
            DefaultRequestHeaders.Add("api_key", "//TODO: configure");
            DefaultRequestHeaders.Add("access_token", "//TODO: configure");
            BaseAddress = new Uri("https://cdn.contentstack.io"); //TODO: configure
        }

        public readonly string Environment = "//TODO: configure";
    }
}

In the Blazor Program.cs, I register this class:

namespace blazor
{
    using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using System.Threading.Tasks;

    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("#app");
            builder.Services.AddScoped(sp => new ContentstackHttpClient());
            await builder.Build().RunAsync();
        }
    }
}

If I inject the class into a Blazor page, I can call HTTPS APIs, in this case just rendering the raw JSON of an entry with a hard-coded content type and ID, just to see if it is possible (and also using Newtonsoft.Json.Linq, just to see if that is possible, and to pretty-print the JSON).

@page "/fetchdata"
@inject ContentstackHttpClient Contentstack

<pre>@message</pre>

@code
{
    string message = String.Empty;

    protected override async Task OnInitializedAsync()
    {
        // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#single-entry
        try
        {
            await Contentstack.GetAsync(
                "v3/content_types/aa/entries/blt8c3febf4493333cf?environment="
                    + Contentstack.Environment).ContinueWith(async r =>
            {
                message = await r.Result.Content.ReadAsStringAsync();
            });
        }
        catch (Exception ex)
        {
            message = ex.GetType() + " : " + ex.Message + Environment.NewLine + ex.StackTrace;
        }
    }
}

2 thoughts on “Call Contentstack HTTPS JSON APIs from ASP.NET Blazor Clients

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: