Skip to content

feat(http): Add redirected property to HttpResponse and HttpErrorResp… #62675

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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);
    }
  }
});

…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.
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: common/http Issues related to HTTP and HTTP Client detected: feature PR contains a feature commit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant