1
votes

I ran into some issues with CORS when setting up my Blazor client-side API client to make requests. I think I found the solution to that, but the solution is also throwing errors.

The main error is: "WASM: System.Net.Http.HttpRequestException: TypeError: Failed to execute 'fetch' on 'Window': The provided value '2' is not a valid enum value of type RequestCredentials."

the code is

        string link = API_RequestLoginTokenEndPoint;

        Http.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample");
        Http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "basic:testuser:testpassword");

        var requestMessage = new HttpRequestMessage(new HttpMethod("GET"), link);

        requestMessage.Properties[WebAssemblyHttpMessageHandler.FetchArgs] = new
        {
            credentials = FetchCredentialsOption.Include
        };

        var response = await Http.SendAsync(requestMessage);
        var responseStatusCode = response.StatusCode;
        var responseBody = await response.Content.ReadAsStringAsync();

        output = responseBody + " " + responseStatusCode;

I also tried changing the request message to: var requestMessage = new HttpRequestMessage(HttpMethod.Get, link);

In case this was the ENUM the error referred to. In the Startup ConfigureServices I tried to add: WebAssemblyHttpMessageHandler.DefaultCredentials = FetchCredentialsOption.Include;

I am using Blazor preview 9. I also tried adding some CORS code to my PHP script on the API route that should accept all origins, but the last question I posted I was told to use this method to fix the CORS problem, which now gives me a new error.

Am I doing something wrong or am I missing something? The error in the browser usually points to the line with the async request: var response = await Http.SendAsync(requestMessage);

2

2 Answers

1
votes

This is a bug not yet fixed. Use this instead :

requestMessage.Properties[WebAssemblyHttpMessageHandler.FetchArgs] = new
        {
            credentials = "include"
        };
0
votes

I had a similar issue and could not resolve the pre-flight activity.

I WAS ABLE TO SOLVE THIS BY COMMENTING OUT HTTP REDIRECTION MIDDLEWARE.

context: Blazor client calls asp.net.core api in different url.

Not sure if this a good solution but I felt I needed to mention this after spending 1 week on this frustrating issue! Hope it helps someone.