Skip to content

Conversation

@SkyZeroZx
Copy link
Contributor

@SkyZeroZx SkyZeroZx commented Jul 17, 2025

HttpResponse redirected Property Support

This commit adds support for the Fetch API's redirected property in HttpResponse and HttpErrorResponse when using HttpClient with the withFetch provider.

This change builds on the functionality introduced in #62315, which added support for passing the redirect option to the Fetch API. With redirects now configurable, exposing the redirected property provides developers with a way to determine if a response was the result of a redirect, further aligning HttpClient's behavior with the native Fetch API and enhancing observability of request flow

The Change Includes:

  • Added redirected property to HttpResponse and HttpErrorResponse classes
  • Updated FetchBackend to capture and forward the redirected flag from the native fetch response
  • Maintains consistency with the Fetch API specification
  • Added unit tests to ensure the property is correctly captured and exposed

Motivation / Use Cases

The redirected property provides valuable information about whether the response was the result of a redirect:

  • Security: Detect when requests have been redirected, which is important for security-sensitive applications
  • Analytics: Track redirect behavior for performance monitoring and debugging
  • Conditional Logic: Implement different handling based on whether a response was redirected
  • API Compliance: Maintain consistency with the native Fetch API behavior

Examples of New Usage

Basic Redirect Detection

import { HttpClient } from '@angular/common/http';

constructor(private http: HttpClient) {}

// Detect if the response was redirected
this.http.get('/api/data' ,   { observe: 'response' } ).subscribe({
  next: (response) => {
    if (response.redirected) {
      console.log('Response was redirected');
      console.log('Final URL:', response.url);
    }
  }
});

Conditional Logic Based on Redirects

// Handle different scenarios based on redirect status
this.http.get('/api/user-profile'  ,   { observe: 'response' } ).subscribe({
  next: (response) => {
    if (response.redirected) {
      // User might have been redirected to login page
      this.handlePossibleAuthRedirect(response);
    } else {
      // Direct response, process normally
      this.processUserProfile(response.body);
    }
  }
});

@pullapprove pullapprove bot requested a review from kirjs July 17, 2025 00:30
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: common/http Issues related to HTTP and HTTP Client labels Jul 17, 2025
@ngbot ngbot bot added this to the Backlog milestone Jul 17, 2025
@SkyZeroZx
Copy link
Contributor Author

@JeanMeche it's possible to try to run it again because failed the last time

…onse

Add support for the Fetch API's redirected property in HttpResponse and HttpErrorResponse when using HttpClient with the withFetch provider.

The redirected property indicates whether the response was the result of an HTTP redirect, providing valuable information for security, debugging, and conditional logic.
@SkyZeroZx SkyZeroZx force-pushed the add-redirect-option-http-response branch from a77a3ce to 7cd4b65 Compare July 20, 2025 19:43
@SkyZeroZx
Copy link
Contributor Author

@JeanMeche I did a rebase, can I try to run the tests again?

Copy link
Contributor

@thePunderWoman thePunderWoman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

reviewed-for: fw-general, public-api

@pullapprove pullapprove bot requested review from devversion and mmalerba July 24, 2025 12:32
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

@thePunderWoman thePunderWoman added target: minor This PR is targeted for the next minor release action: merge The PR is ready for merge by the caretaker labels Jul 24, 2025
@thePunderWoman
Copy link
Contributor

This PR was merged into the repository by commit 0984b30.

The changes were merged into the following branches: main

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: common/http Issues related to HTTP and HTTP Client detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants