简体   繁体   中英

How do I remove these custom query parameters from my RESTANGULAR HTTP Request?

I'm trying to remove custom parameters from the following HTTP request on my REST API:

I want to turn http://localhost:3000/users?_page=1&_perPage=30&_sortDir=DESC&_sortField=id

into http://localhost:3000/users

I am using ng-admin, which is an AngularJS admin panel, they provide a page on changing query parameters here: https://github.com/marmelab/ng-admin/blob/master/doc/API-mapping.md

I have used some of their code and tried to use the following code to implement what I'm trying to do but it won't work.

myApp.config(['RestangularProvider', function(RestangularProvider) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
    if (operation == 'getList' && what == 'entityName') {
        delete params._page;
        delete params._perPage;
        delete params._sortField; 
        delete params._sortDir; 
    }
    console.log({ params: params });
});
}]);

Finally, how can I check the actual HTTP request that is sent through once the interceptors have been utilised, chrome developer tools seems to only display the original request with all parameters even though I've implemented the above methods. I think this is because the interceptor works after the browser implements the request.

You should return whole request from interceptor otherwise there won't be any changes on request. It is written at documents.

addFullRequestInterceptor

addFullRequestInterceptor

This adds a new fullRequestInterceptor. The fullRequestInterceptor is similar to the requestInterceptor but more powerful. It lets you change the element, the request parameters and the headers as well.

It's a function that receives the same as the requestInterceptor plus the headers and the query parameters (in that order).

It can return an object with any (or all) of following properties:

headers: The headers to send params: The request parameters to send element: The element to send httpConfig: The httpConfig to call with If a property isn't returned, the one sent is used.

at the last lines it said If a property isn't returned, the one sent is used. so if you send changed properties in an object then you should be fine...

myApp.config(['RestangularProvider', function(RestangularProvider) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
    if (operation == 'getList' && what == 'entityName') {
        delete params._page;
        delete params._perPage;
        delete params._sortField; 
        delete params._sortDir; 
    }
    console.log({ params: params });

    // return changed properties which is params in this case
    return { params: params };


});
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM