Hi support,
I want to use the new feature
applyPreRequestMiddlewares with my DataManager to poulate a grid.
I am using:
But no extra parameters or headers are sent to my backend if the grid is refreshed (and "dataremark" ist called).
What am I doing wrong?
Best wishes,
Stephan
Hi Stephan,
Greetings from Syncfusion support,
Based on the query, we understand that you are trying to send a token and extra parameters and want to add the header to the request by applying middleware logic using the DataManager. Upon inspecting the provided code example, we observed that the header is being modified directly from the request using ‘context.request’. Instead, you must reassign the headers to the request, as shown in the example below. This is the reason for the reported behavior.
Please refer the below code example:
|
let dataManager = new ej.data.DataManager({ url: 'https://services.syncfusion.com/js/production/api/UrlDataSource', adaptor: new ej.data.UrlAdaptor(), });
// Middleware before request is sent to the server dataManager.applyPreRequestMiddlewares = async function (request) { return { headers: [ { Authorization: 'Bearer your-access-token' }, { Videotime: '8899' }, // Extra parameters via header ], }; };
|
Sample: https://stackblitz.com/edit/gouk4dbx-lnsxekaa?file=index.js
Screenshot:
Regards,
Mohanraj Rengasamy
Hi Mohanraj,
many thanks for your quick answer.
It works now :)
Perhaps the example in your documentation can be updated to reflect this solution.
Is there also a possibility to add request parameters to the request via applyPreRequestMiddlewares?
Best wishes,
Stephan
Stephan,
We are happy to hear that our provided solution was helpful.
Query#: Is there also a possibility to add request parameters to the request via applyPreRequestMiddlewares?
Yes, you can include request parameters within the applyPreRequestMiddlewares method as shown below. Please refer to the code example, sample and screenshot for further details.
|
Index.js
// Middleware before request is sent to the server dataManager.applyPreRequestMiddlewares = async function (request) { let modifedPayload = JSON.parse(request.data); modifedPayload.Foo = "Hello World!"; let senddata = JSON.stringify(modifedPayload); request.data = senddata; return { headers: [ { Authorization: 'Bearer your-access-token' }, { Videotime: '8899' }, // Extra parameters via header ] }; };
|
Sample: https://stackblitz.com/edit/gouk4dbx-ekwrwuf9?file=index.js
Screenshot:
Hello,
many thanks for this code.
It works perfect to get and update the data.
The necessary parameters are added to the query :)
But at inserting new data the parameters are not added :(
Im using these settings of my dataManager
This dataManager has the PreRequestMiddlewares Settings as you mentioned and is bound to a grid.
But if I want to add a new entry to the grid, the extra parameters are not added and it looks like the async function within the applyPreRequestMiddlewares is not invoked at all (console.log is not executed)
Regards,
Stephan
dataManager.applyPreRequestMiddlewares method is properly invoked, and the custom parameters are passed correctly. We have also attached a video demo for your reference. Kindly review it once. var hostUrl = 'Home/UrlDataSource'; var data = new ej.data.DataManager({ url: hostUrl, insertUrl: 'Home/Insert', updateUrl: 'Home/Update', removeUrl: 'Home/Remove', adaptor: new ej.data.UrlAdaptor(), // Use your custom adaptor here crossDomain: true }); data.applyPreRequestMiddlewares = async function (request) { let modifedPayload = JSON.parse(request.data); modifedPayload.Foo = 'Hello World!'; let senddata = JSON.stringify(modifedPayload); request.data = senddata; return { headers: [ { Authorization: 'Bearer your-access-token' }, { Videotime: '8899' }, // Extra parameters via header ], }; }; |
Thanks für your example.
Are you sure, that you did analyse the request of the insert action in your example?
If there is an insert action with a grid you will never get the request parameter "requiresCounts".
This parameter only occurs at retrieving data via the "url" parameter of the assigned DataManager.
At the insert action you will always get the parameter "action: insert" together with the parameters of the new row of the grid.
After an insert, the grid automatically refreshes itself in calling the "url" parameter. Here the parameters defines in the Middleware are sent as data of the request.
But just right before that, the url defined at "insertUrl" is called. And here the extra parameters from the Middleware are missing.
Can you please check this?
BTW: With the grid action "update" everything is ok, the extra parameters defined in the Middlware are sent.
Regards,
Stephan
Hi ,
Sorry for the inconvenience,
We have confirmed the reported scenario as a bug from our end and logged it as “The applyPreRequestMiddlewares does not triggers during insert operation”. Thank you for taking time to report this issue and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technical feasibility and Product Development Life Cycle ) and will include the defect fix in our upcoming patch release scheduled for July 01st, 2025.
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through the below link,
Disclaimer: "Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization".
Regards,
Kishore Muthukrishnan
Hi,
many thanks for your support.
Unfortunately the link to the feedback does not work (not associated to my account), so I don't know when the fix is included.
It looks like it ist not in the release from June 25.
Regards,
Stephan
Hi ,
We are glad to announce that, we have included the fix for the issue “The applyPreRequestMiddlewares does not triggers during insert operation ” in our 30.1.38 release. So please upgrade to our latest version of the Syncfusion package to resolve the reported issue.
Root Cause: The error is caused because the middlewares were not triggered before request send to the server.
Solution: The issue is resolved by use the middlewares on before sending the request to server.
We thank you for your support and appreciate your patience in waiting for this release. Please get back to us if you need any further assistance.
Regards,
Kishore Muthukrishnan
Hi,
this is good news, many thanks!
Can you please add this version to the CRG ?
Regards,
Stephan
Hi ,
We have added the v30.1.38 in CRG and you can download it from here
Regards,
Kishore Muthukrishnan