Aspnetcore: [Blazor WebAssembly] Unable to perform request with mode: no-cors

Created on 22 Apr 2020  路  3Comments  路  Source: dotnet/aspnetcore

Describe the bug

Using the HttpClient I'm unable to perform a request with "mode: no-cors"

To Reproduce

The following is a working Fetch call from Javascript.

fetch("https://nl.wiktionary.org/wiki/Speciaal:WillekeurigeUitCategorie/Woorden_in_het_Nederlands", {
  "headers": {},
  "referrer": "https://localhost:5001/counter",
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": null,
  "method": "GET",
  "mode": "no-cors",
  "credentials": "omit"
});

This results in the following request headers (among others):

sec-fetch-dest: empty
sec-fetch-mode: no-cors
sec-fetch-site: cross-site

I'm unable to add the listed headers to my request since they are part of Forbidden Header Names.
HttpRequestMessage does not support a fetch mode, and adding the "mode" header itself does not work as well.

            using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage() {
                    RequestUri = new Uri(config.Random),
                    Method = HttpMethod.Get,
                };
                request.Headers.Add("mode", "no-cors");
                var result = await client.SendAsync(request);
                return await result.Content.ReadAsStringAsync();
            }

Relevant documentation

Further technical details

dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 3.1.201
Commit: b1768b4ae7

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.201\

Host (useful for support):
Version: 3.1.3
Commit: 4a9f85e9f8

.NET Core SDKs installed:
3.1.201 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Answered Resolved area-blazor blazor-wasm question

Most helpful comment

@StefanJanssen95 support for this is being added as part of the preview5 release: https://github.com/dotnet/aspnetcore/blob/blazor-wasm/src/Components/WebAssembly/WebAssembly/src/Http/WebAssemblyHttpRequestMessageExtensions.cs#L81. It'll look something along these lines: https://github.com/dotnet/AspNetCore.Docs/pull/17933/files#diff-307f8e7491f323b15e07ee965c875d0bR222

All 3 comments

@StefanJanssen95 thanks for contacting us.

There is a way to do this, but it's not by setting a header. @pranavkm knows the details.

@StefanJanssen95 support for this is being added as part of the preview5 release: https://github.com/dotnet/aspnetcore/blob/blazor-wasm/src/Components/WebAssembly/WebAssembly/src/Http/WebAssemblyHttpRequestMessageExtensions.cs#L81. It'll look something along these lines: https://github.com/dotnet/AspNetCore.Docs/pull/17933/files#diff-307f8e7491f323b15e07ee965c875d0bR222

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Was this page helpful?
0 / 5 - 0 ratings