How do I embed a 3rd Party API service inside my ASP.net Blazor Web Assembly

Ryan George 1 Reputation point
2021-07-06T08:04:23.74+00:00

Hello,

I was in the process of making a website that features a UI displaying information from a 3rd Party API

When the client refreshes the page: Client Browser requests XML from 3rd Party API -> Client then parses XML Response -> Client then outputs the data (All Asynchronously)

As far as I have read in the Microsoft Learn they set up their own API service using their own data from the server, but since my XML is rather large calling an API which then calls another API would result in a large download time overall..

How should I implement the request to a 3rd Party API directly?

I have fully developed the backend already in C#, the request looks something like this:

public string XMLRequest;

public static async Task<string> executeRequest()  
        {  
  
            HttpWebRequest client = (HttpWebRequest)WebRequest.Create(3RDPARTYURL);  
  
            client.ContentType = "application/soap+xml";  
  
            client.Method = "POST";  
            using (Stream postStream = await client.GetRequestStreamAsync())  
            {  
                byte[] postBytes = Encoding.ASCII.GetBytes(XMLRequest);  
                await postStream.WriteAsync(postBytes, 0, postBytes.Length);  
                await postStream.FlushAsync();  
            }  
              
  
            Task<string> Response;  
            using (var response = (HttpWebResponse)await client.GetResponseAsync())  
            using (Stream streamResponse = response.GetResponseStream())  
            using (StreamReader streamReader = new StreamReader(streamResponse))  
            {  
                Response = streamReader.ReadToEndAsync();  
            }  
  
            return await Response;  
  
}  

Ryan

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,162 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce Barker 801 Reputation points
    2021-07-07T22:15:33.29+00:00

    the main requirement is the the 3rd party url support CORS for your site. because the browser will restrict access (as blazor is just using javascript to make ajax calls). if the 3rd party does supports CORS, then you will need a server proxy.

    0 comments No comments