Skip to content

Conversation

SkyZeroZx
Copy link
Contributor

HttpResponse responseType Property Support

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

This change enhances HttpClient's alignment with the native Fetch API by exposing the response type information that indicates how the browser handled the response based on CORS policies and request mode. This provides developers with valuable insights into the nature of the response they received.

The Change Includes:

  • Added responseType property to HttpResponse and HttpErrorResponse classes
  • Maintains consistency with the Fetch API specification
  • Added comprehensive unit tests to ensure the property is correctly captured and exposed

Motivation / Use Cases

The responseType property provides crucial information about how the browser handled the response based on CORS policies and request configuration:

  • CORS Debugging: Understand why certain headers or response data might be inaccessible
  • Security Analysis: Identify opaque responses that may indicate cross-origin issues
  • Request Mode Validation: Verify that requests are being handled according to the expected CORS mode
  • API Compliance: Maintain consistency with the native Fetch API behavior
  • Performance Monitoring: Track different response types for analytics and optimization

Examples of New Usage

// Debug CORS issues by checking response type
this.http.get('https://external-api.com/data', { observe: 'response' }).subscribe({
  next: (response) => {
    if (response.responseType === 'opaque') {
      console.warn('Response is opaque - headers and body may not be accessible');
      console.warn('Consider enabling CORS on the server or using a proxy');
    } else if (response.responseType === 'cors') {
      console.log('CORS is properly configured');
      // Safe to access response headers and body
      this.processResponse(response);
    }
  }
});

@pullapprove pullapprove bot requested a review from mmalerba August 7, 2025 02:03
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: common/http Issues related to HTTP and HTTP Client labels Aug 7, 2025
@ngbot ngbot bot added this to the Backlog milestone Aug 7, 2025
@SkyZeroZx
Copy link
Contributor Author

Question : I still have doubts about the name of the property, although responseType seems correct to me, since type has already been used previously

@SkyZeroZx SkyZeroZx force-pushed the add-mode-http-response branch from 006ccfd to 5c7b86d Compare August 7, 2025 02:07
…ponse

Add support for the Fetch API's responseType property in HttpResponse and HttpErrorResponse when using HttpClient with the withFetch provider.
@SkyZeroZx SkyZeroZx force-pushed the add-mode-http-response branch from 5c7b86d to 0121b56 Compare August 7, 2025 02:07
@SkyZeroZx
Copy link
Contributor Author

@mmalerba Is there anything else missing for this Pull Request to be approved or any observations?

Copy link
Contributor

@mmalerba mmalerba left a comment

Choose a reason for hiding this comment

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

Sorry, I was on vacation for a bit and it fell off my radar. LGTM

@mmalerba mmalerba added the action: merge The PR is ready for merge by the caretaker label Aug 27, 2025
Copy link
Contributor

@mmalerba mmalerba 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

@pullapprove pullapprove bot requested a review from kirjs August 27, 2025 18:30
Copy link
Member

@JeanMeche JeanMeche 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

@JeanMeche JeanMeche added the target: minor This PR is targeted for the next minor release label Aug 27, 2025
@JeanMeche JeanMeche removed request for atscott and kirjs August 27, 2025 18:31
@mmalerba
Copy link
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

@mmalerba mmalerba closed this in 07e6788 Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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