Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Failed to execute 'fetch' in Blazor Webassembly #2248

Open
fabiohcnobre opened this issue Jul 24, 2024 · 7 comments
Open

TypeError: Failed to execute 'fetch' in Blazor Webassembly #2248

fabiohcnobre opened this issue Jul 24, 2024 · 7 comments

Comments

@fabiohcnobre
Copy link

I am using the code below

var options = new RestClientOptions("http://apibihml.value4u.com.br");
var client = new RestClient(options);
var request = new RestRequest("/obter-acesso/", Method.Get);
request.AddHeader("Host", "apibihml.value4u.com.br");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddHeader("AcceptCharset", "utf-8");
RestResponse response = await client.ExecuteAsync(request);
Console.WriteLine(response.Content);

Erro in blazor

▶ | ErrorMessage | 'TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.' |

image

image

  • OS: Windows
  • .NET version 8
  • Blazor Webassembly
@alexeyzimarev
Copy link
Member

Yes, GET requests cannot have a body. Why are you setting the content type header if your request has no content???

@fabiohcnobre
Copy link
Author

Even without a header it gives an error.

image

I created this repository for testing.
https://github.com/fabiohcnobre/TestBlazor.BI.Web

@alexeyzimarev alexeyzimarev removed the bug label Aug 13, 2024
@alexeyzimarev
Copy link
Member

I am wondering if you read the docs https://restsharp.dev/docs/usage/client#blazor-support

@fabiohcnobre
Copy link
Author

Yes,
Same error using the information. You can see the error in the repository below.
https://github.com/fabiohcnobre/TestBlazor.BI.Web

@alexeyzimarev
Copy link
Member

Sorry, but I don't really understand you. Your repository has this code on the page:

            var options = new RestClientOptions("https://apibihml.value4u.com.br");
            var client = new RestClient(options);

The documentation clearly says that you need to register the client using the Blazor version of HttpClient:

builder.Services.AddSingleton(new RestClient(new HttpClient()));

followed by a page example where you use the injected client.

The docs additionally points out that:

You need to remember that webassembly has some platform-specific limitations. Therefore, you won't be able to instantiate RestClient using all of its constructors. In fact, you can only use RestClient constructors that accept HttpClient or HttpMessageHandler as an argument.

I don't see any of this in your repository.

@alexeyzimarev
Copy link
Member

In addition, calling the API in Postman doesn't really help. Blazor HttpClient uses the fetch function, and the best way to understand what's going on is to open the dev tools in the browser and look at both console and network calls tabs. You'll see if the request actually has a body, what the server returns (if anything), or if the fetch hasn't been executed, as well as the error message from the fetch call.

@fabiohcnobre
Copy link
Author

@alexeyzimarev

I hadn't updated github because it gave the same error. But I updated the repository and the same error continues.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants