dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

[browser] httpClient response code 0 #106689

Open samosim opened 2 months ago

samosim commented 2 months ago

Is there an existing issue for this?

Describe the bug

Here is my test code in Blazor for HttpClient:

var user = new Register1kaUser("Samo","samosimoncic@gmail.com","Gazelca.5", "Gazelca.5", 1, "Registracija");

var userJson = JsonSerializer.Serialize(user);

var handler = new HttpClientHandler()
{
    AllowAutoRedirect = false,           
};        

 var httpClient = new HttpClient(handler);       

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, "https://mladim1ka.azurewebsites.net/1ka/frontend/api/api.php?action=register")
{
    Content = new StringContent(userJson),
};

message.Content.Headers.Add("Content-Length", $"{userJson.Length}");

var response = await httpClient.SendAsync(message);

the reponse code is 0 and location is null. If I use the same code in Console application I get redirect code response with location. It is worth mentioning that redirect code together with location are shown in browser under the network tap. Additionally, I test the same request in Postman and it works just fine. Please let me know what the problem with Blazor for code response 0 is.

Expected Behavior

I should get the response code which reflects redirection together with location path.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

net 7.0

Anything else?

No response

javiercn commented 2 months ago

@samosim thanks for contacting us.

Very likely you need to set the option in the underlying fetch request. We suggest you also check the network tab to see if the response came back as opaque, preventing JS from seeing it. It might also be a limitation on the browser sandbox.

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

samosim commented 2 months ago

@samosim thanks for contacting us.

Very likely you need to set the option in the underlying fetch request. We suggest you also check the network tab to see if the response came back as opaque, preventing JS from seeing it. It might also be a limitation on the browser sandbox.

Which options do you suggest to set? Attached, please find the snapshot from the network tab. Do you see any issues from this image? request

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

antonfirsov commented 2 months ago

cc @pavelsavara

campersau commented 2 months ago

AllowAutoRedirect = false will set redirect = "manual" https://github.com/dotnet/runtime/blob/56435ad8ce8cb1f6511d837d6b86707eeaf46ac3/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs#L188-L189 https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#redirect

Which will strip out a lot of information from the response https://github.com/dotnet/runtime/blob/56435ad8ce8cb1f6511d837d6b86707eeaf46ac3/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs#L311-L313 https://developer.mozilla.org/en-US/docs/Web/API/Response/type

I am not sure if AllowAutoRedirect can currently be implemented correctly since redirect = "manual" does not what everybody thinks it does. I also learned that here: https://github.com/dotnet/aspnetcore/pull/52559#discussion_r1414162643

See also: