restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Tie up request and response with unique ID

Open MilosMosovsky opened this issue 10 years ago • 3 comments

Hello @mgonto , i found that you had similiar question here. How to detect from response which request invoked it.

It has a lot of usecases when you need tie-tip request/response with unique ID. For example logging of every query, ajax-loader for corrensponding request.

You told on old issue there is way to send/receive custom headers and read them in interceptor. But i think its not correct way to adjust 3rd party backend to "detect" which request and response are tied when another 3rd library (restangular) is actually invoker of request. Restangular should be responsible to know which request is tied to which response.

What do you think about adding new property (something like requestId) to an element.

 function elemFunction(operation, what, params, obj, headers) {
          var restangularId = _.uniqueId();
------
 var okCallback = function(response) {
   _.extend(response, { restangularId : restangularID});
}

var errorCallback = function(response) {
     _.extend(response, { restangularId : restangularID});
}

This is solve number one , but if you dont want decorating "response" because response should be response from server, what about manually "injecting" this header inside "headers" object?

For this time i solved it by altering your library but i am asking for official support so i can make PR :)

Let me know what do you think about it.

MilosMosovsky avatar Jun 16 '15 07:06 MilosMosovsky

Hi I am looking for a feature similar to the one you mention, could you post a link to the issue on which mgonto mentioned about the custom headers?

Altaflux avatar Jun 16 '15 15:06 Altaflux

@Altaflux https://github.com/mgonto/restangular/issues/517 . If you wanna more info, you just create RequestInterceptor which is extending headers with some custom paramater , for example X-Restangular-Request, and on backend side you will just "forward them". then in ResponseInterceptor you can read it. But i dont like it, Actually today i forked restangular with custom code , but waiting for @mgonto to response.

MilosMosovsky avatar Jun 16 '15 17:06 MilosMosovsky

I need the same feature as you. I tried mgonto's way and it's only a workaround. BTW, do you know if it's possible to "reject" a response in responseInterceptor? Because in my use case I want to "reject" all the response from my "old requests". But for now I could only do the following things:

  1. Find a response is from an "old" requests, it is junk and should be rejected. I won't return it.
  2. Since in the doc it says "The responseInterceptor must return the restangularized data element", I will still get a response which is undefined in the "success function".
  3. if (typeof response === 'undefined') { return; }

Is it possible to stop the process in the responseInterceptor? Thank you!

cwang1221 avatar Nov 23 '16 03:11 cwang1221