HttpClient.SendAsync on Blazor WASM returns empty HttpResponseMessage although Fiddler shows that correct response was received

1k Views Asked by At

My Blazor page code:

private async Task OnSendSMSClick()
    {
        var request = new HttpRequestMessage(HttpMethod.Post, baseAddress);
        request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
        request.SetBrowserRequestCache(BrowserRequestCache.NoCache);
        HttpContent content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("client_id", client_id),
                new KeyValuePair<string, string>("client_secret", client_secret)
            });
        content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
        request.Content = content;
        RequestOutput = request.ToString();

        HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);
        
        ResponseOutput = response.StatusCode.ToString();
    }

response always returns empty although I see on Fiddler that the actual result is returned. On Google Chrome, I see that the server responded 200 with correct headers but somehow Chrome says "Failed to load response data".

Fiddler SS

No errors on Chrome and I get a 200 response but it somehow fails to show the response content:

Chrome 200

Chrome fails to display response content

And you can see in the debugger that the response object is empty..

Empty response object

How can I fix that, what I am missing here?

Thank you!

1

There are 1 best solutions below

0
Steve C On

Blazor has an issue with sites requiring CORS (source).

Unfortunately, there's nothing that can be done on the Blazor client side.
The only solution I've found is to either disable CORS on the server or set up a proxy server outside of Blazor.