Consuming REST API from WASM project in Visual Studio 2019

Posted 70 days ago by Bassonette

I am using basic authorization to consume third party API from my WASM project. However in developer tools I see HTTP 403 error. I am using my usual way of authenticating and in postman these credentials work. Is basic auth possible in WASM ? My example code technique is here:

@page "/"
@inject HttpClient TestClient
<h3>Presentations Scheduled</h3>

   <fieldset class="form-group">
           <button type="button" class="btn btn-dark mr-sm-2 mb-2"
                   @onclick=@(async ()=>  await GetToday())>

   @if (string.IsNullOrWhiteSpace(errorString) == false)
       <div class="h2">@errorString</div>
      <div class="h2">@httpResponse</div>

    string errorString;
    string httpResponse;

   private async Task GetToday()
        var byteArray = Encoding.ASCII.GetBytes("user:pass");                
        TestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",Convert.ToBase64String(byteArray));
        string todayDate = DateTime.Today.AddDays(0).ToString("yyyyMMdd");
        string TestbaseURL = "";
        UriBuilder TestURI = new UriBuilder(TestbaseURL);
        TestURI.Scheme = "https";
        TestURI.Path = "ws/run/reservations.json";
        TestURI.Query = "resource_query_id=244782";
        var Testquery = HttpUtility.ParseQueryString(TestURI.Query);
        Testquery["scope"] = "extended";
        Testquery["start_dt"] = todayDate;
        Testquery["end_dt"] = todayDate;
        TestURI.Query = Testquery.ToString();
        TestbaseURL = TestURI.ToString();

            httpResponse = await TestClient.GetStringAsync(TestbaseURL);
            errorString = null;
       catch (Exception ex)
           errorString = $"There was a problem: {ex.Message}";


  • 0

    Actually I think this problem is a CORS issue -- In developer tools I am seeing "CORS Missing Allow Origin" --- so I am working on getting past this now... 

    Posted 70 days ago by Bassonette
  • 0

    Does anyone know how to fix this problem from the client side ? 

    Posted 70 days ago by Bassonette
  • 1

    I haven't ran into this problem myself, but you may either need to get the third party API to whitelist your website to get the response to include the right headers (may not have much luck there). A more likely option may be to make a call to your own API that calls the third party API and return the data to your WebAssembly app. That should get you around the CORS issue.

    Posted 64 days ago by selliott
  • 0

    Thanks Selliott. I've since traced this issue down to a CORS prooblem when trying to access third party API's when using a WASM application and found that all requests run from the browser. When I switched my Blazor app over to a server app, the issue resolved as the requests from the API ran from the server endpoint and not in the browser. I am going to consider this closed.. 

    Posted 63 days ago by Bassonette
Someone is typing...

Post a Reply

You must be logged in to add a new post.
Number of online users: 0
An error has occurred. This application may no longer respond until reloaded. Reload 🗙