Skip to content

Allow passing misc data to http client options #18155

Closed

Description

@mattlewis92

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Currently when using a http interceptor you cannot pass meta data about the request to the interceptor

Expected behavior

To be able to pass random data to the http interceptor. Currently the options object is so strongly typed that you cannot pass extra data to it. The proposal would be to add a meta (name needs bikeshedding) property with type any to the http request options that can be used for this purpose.

Minimal reproduction of the problem with instructions

N/A

What is the motivation / use case for changing the behavior?

// tell a caching interceptor to only cache if this is true
this.http.get('/path', {meta: {cache: true}})

// tell an interceptor not to add the `Authorization` for this request only
this.http.get('/path', {meta: {skipAuthorization: true}})

Environment


Angular version: 4.3.0


Browser:
- [x] Chrome (desktop) version 59
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Others:

Activity

  1. jnizet commented on Jul 17, 2017

    @jnizet
    Contributor

    This would also be very useful for generic error interceptors:

    // tell an interceptor that 404 error is expected and shouldn't be displayed
    this.http.get('/path', {meta: {skipErrorHandling: resp => resp.status === 404}})
    

    I see plenty of use-cases for this, and it was something possible in AngularJS using the options object.

  2. added
    area: common/http
    feature
    on Jul 19, 2017
  3. alxhub commented on Jul 19, 2017

    @alxhub
    Member

    This has been on my mind as well. I think it's a good idea and something we should implement.

  4. mattlewis92 commented on Jul 19, 2017

    @mattlewis92
    ContributorAuthor

    @alxhub awesome! What do you think of the proposed API, can you think of anything better? I'd love to take a go at a PR for this 😀

  5. alxhub commented on Jul 25, 2017

    @alxhub
    Member

    @mattlewis92 I like meta (or possibly metadata) as the key. In keeping with the theme for HttpRequest and HttpResponse it should also be immutable a la HttpHeaders / HttpParams.

    I have an open task to merge the implementation of the two by extracting out a generic immutable map type. Once this is done, HttpMetadata could easily be built on top of that type.

  6. mattlewis92 commented on Jul 25, 2017

    @mattlewis92
    ContributorAuthor

    Ah ok I'll leave that to you to implement then 😄

  7. Agrumas commented on Aug 18, 2017

    @Agrumas

    Is there any neat workaround for this? I am thinking about extending HttpRequest, however, overloaded constructors make implementation complicated.

  8. ChristianUlbrich commented on Aug 30, 2017

    @ChristianUlbrich

    @Agrumas You could abuse HttpHeaders. Set them on your request with a nice name e.g. X-META-DATA and then strip them in the interceptor from the actual request before it gets send and re-add them on response. That way I plan to enable disabling individual interceptors on a per-request basis, e.g. for toggling a global LoadingNotification. But of course this feature needs to be added - because it has so many use cases.

  9. alxhub commented on Aug 30, 2017

    @alxhub
    Member

    I have an in-progress branch for this, but it's unlikely to land before 5.0. Probably coming in 5.1.

  10. jbojcic1 commented on Oct 14, 2017

    @jbojcic1
  11. sinedied commented on Nov 6, 2017

    @sinedied

    Really disappointed that with the release of Angular 5, the team deprecated the Http service without providing any way to perform with HttpClient what was possible with Http, including custom parameters.

    We currently extends the Http service and added with 2 extra parameters to RequestOptionsArgs, for common use cases already cited here such as caching requests and error handling (see https://github.com/ngx-rocket/starter-kit/blob/master/src/app/core/http/http.service.ts for details).

    We would like to move to HttpClient, but we cannot use interceptors because they do not support custom arguments, and we cannot extend HttpClient either because the request() method options do not use a type we can extend, see https://github.com/angular/angular/blob/master/packages/common/http/src/client.ts#L76-L83

    So basically we are stuck with a deprecated API, and no way to move forward, am I missing something here @alxhub ?

  12. karptonite commented on Nov 6, 2017

    @karptonite
    Contributor

    @sinedied I've been using HttpClient and adding metadata by abusing HTTP Headers, as suggested here.

    In practice, it looks like this:

        if (req.headers.has('Skip-Prefix')) {
          const headers = req.headers.delete('Skip-Prefix');
          const directRequest = req.clone({ headers });
          return next.handle(directRequest);
        }
  13. sinedied commented on Nov 6, 2017

    @sinedied

    @karptonite I saw the comment on this, but this is really a ugly hack and I would like to avoid those while promoting best practices in our generator 😉

  14. karptonite commented on Nov 6, 2017

    @karptonite
    Contributor

    @sinedied It isn't pretty, but I've seen far uglier hacks. You can extract out the addition of the headers into a utility function so that when the metadata is added for real, you only have to fix it in a couple places. Anyway, you aren't stuck with a deprecated (but not yet removed) API--you have at least one option.

  15. 67 remaining items

  16. maxandriani commented on Nov 17, 2020

    @maxandriani

    @alxhub Is there something we can do to Help on this issue? I saw there is an PR that you was involved that looks great. Why you guys can not aprove it? I know there is a issue about patterns that should be argued. But we live on agile environment w/ incremental features. You can release a initial version and fix it as the problem happens.

  17. sebastian-zarzycki-apzumi commented on Nov 17, 2020

    @sebastian-zarzycki-apzumi

    Operation Byelog eh? I'm sorry to be harsh, but if Angular team is focusing on pretty minor (nowadays cpu/internet performance considered) speed/size improvements and dances around engines and compilers for another couple years, rather that adding meaningful features, then something is way off balance there.

  18. jcimoch commented on Nov 17, 2020

    @jcimoch

    @sebastian-zarzycki-es I have exactly the same feeling over years of using angular. It seems like there are couple of people that are fascinated over compilers, performance, alogrithms but not interested in making usefull framework solving real business problems. Maybe if angular compiler wasn't over engineered in the first place (because AOT in its initial format bascially has taken best advantages of dynamic javascript environment in favour of some perfomances gains which were at the best only close to react performance which is fully runtime based). Now with ivy we are bringing back basic stuff like using arrow function were I want and not were angular compiler allows... or dynamically create/load component fetched from resource out of the project, this wasn't possible in any reasonable way before. So we are solving issues that we created and were solved long time ago, even in angularjs.
    Something is way off balance there as you said.

  19. CesarD commented on Nov 18, 2020

    @CesarD
  20. marcschroeder commented on Jan 16, 2021

    @marcschroeder

    @alxhub is there anything we can do to support you on this issue? Who of the core team is assigned to this? I feel like there have been plenty of people offering there help over the past three years that this issue is open. This is a non-breaking change, right? What can we do to get this into the next minor release?

  21. imaksp commented on Jan 16, 2021

    @imaksp
    Contributor

    As per this comment (on related PR) from angular team member this is not coming anytime soon as it requires code refactoring in core:
    #20579 (comment)

    But 'InterceptorSkipHeader' (although not the best) solution is working fine for me.

    intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
      if (req.headers.has(InterceptorSkipHeader)) {
        // console.log('skiping intercepter');
        const headers = req.headers.delete(InterceptorSkipHeader);
        return next.handle(req.clone({ headers }));
      }
      // any other logic for adding common header
      // ...
    }
  22. alxhub commented on Feb 2, 2021

    @alxhub
    Member

    For anyone keeping up with this issue, discussions on PR #25751 have converged on a simple and straightforward design which we hope to be able to land in the next couple days :)

  23. sebastian-zarzycki-apzumi commented on Feb 2, 2021

    @sebastian-zarzycki-apzumi

    4 years of waiting for a change of heart (for the context to be mutable) to eventually implement a simple typed map. Oh, the Angular team, you never cease to amaze me :)

    (still, thanks for this)

  24. jcimoch commented on Feb 2, 2021

    @jcimoch

    obraz

  25. alxhub commented on Mar 19, 2021

    @alxhub
    Member

    Closing as fixed in #25751 (will land in 12!)

  26. CesarD commented on Mar 23, 2021

    @CesarD

    Closing as fixed in #25751 (will land in 12!)

    Will we have to wait until Angular 12 to make use of this? That's around May, isn't it?

  27. angular-automatic-lock-bot commented on Apr 23, 2021

    @angular-automatic-lock-bot

    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.

  28. locked and limited conversation to collaborators on Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/httpfeaturefreq2: mediumstate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions