What is the error message in Blazor?
Acess REST Service outside actual URL
I had a rest Service (WCF) - I added CORS Support.
In Postmann I can verify that ther is a Access-Control-Allow-Origin and also a Access-Control-Allow-Headers with "*".
I can get acess from console-Programm - but in Blzor there is an Error.
Any Idea or is there a Rest service I can try out to get Acess ?
0 Posted 2 years ago by selliott0 try
{
var ret = await Http.GetFromJsonAsync<string>(url);
if (ret?.Length > 0)
{
rest = ret;
}
}
catch (Exception E)
{
error = E.Message;
}
E.Message means: TypeError: Failed to fetch
Posted 2 years ago by HubertBieder0 Are you sure it is returning a Json response? It would also error out if it wasn't able to parse the response as Json.
While troubleshooting, maybe try something along the lines of
var response = await Http.GetAsync(url); if(response.IsSuccessStatusCode) ret = await response.Content.ReadFromJsonAsync<string>();
And set a breakpoint at the response line or log it in some way to see what the full response is that you're receiving.
Posted 2 years ago by selliott0 It crashes in : var response = await Http.GetAsync(url);
response has value null
Posted 2 years ago by HubertBieder0 Did you allow it to step to the next line after the response? It shouldn't be null if you're hitting a valid url/endpoint. The debugger will say it's null until you step to the next line after it.
Posted 2 years ago by selliott0 Your comments are very helpfuil.
I build a CoreWebAPI Restservice. He has two Adresses
- Http:
- Https
If i used your code on the http Rest there is a crash GetTaSYNC89 and I´m in the Exception Clause.
If I used the http Adress, i get a string not a String in Json. and so it is not possible to convert from Json to string,.
==> I will now ensure that i get a Jason Object in my test Software. In my Legacy sotware i have it fixed that is a Json covered string.
==> Is it not allowed to consume a http Rest ?
==> Is there any Method/Switch to allow http Rest to use ?
==> Especially in Legacy systems I have often only http Rest Services.
Thank you for your support and advice.
Posted 2 years ago by HubertBieder0 This link may be helpful: https://docs.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-5.0&pivots=webassembly
Posted 2 years ago by selliott0 I believe that it is not possible to access a REST Service with http://.
Posted 2 years ago by HubertBieder0 If your non-secure Blazor site isn't able to work with your API, then maybe your API isn't setup to allow non-secure traffic? Are you either redirecting all non-secure URL's to a secure URL, or maybe haven't setup your API Authentication to not require https metadata?
The first two thoughts that come to mind for common setups is to:
- Check whether you have "app.UseHttpsRedirection()" in your Configure method of your Startup.cs file. This would force a redirect.
- If you're using JWT Bearer Tokens, when you setup something like "services.AddAuthentication().AddJwtBearer()" in Startup.cs, make sure you set options.RequireHttpsMetaData = false. See a general example below
services.AddAuthentication() .AddJwtBearer(IdentityServerJwtConstants.IdentityServerJwtBearerScheme, options => { ... options.RequireHttpsMetadata = false; ... });
Posted 2 years ago by selliott