intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { if (!req.url.startsWith("http://") && !req.url.startsWith("https://")) { console.log("Intercepted call: " + req.url) this.apiUrl = this.appSettingsService.GetSettingByName("Configuration.ApiUrl").replace(/\/$/, ""); if (!this.apiUrl) { throw new Error("API URL is empty."); } else { const apiReq = req.clone({ url: `${this.apiUrl}${req.url}` }); return next.handle(apiReq); } } else { console.log("Un-intercepted call: " + req.url) return next.handle(req); } }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { // add authorization header with jwt token if available if (this.tokenService.tokenExists()) { let token = this.tokenService.getToken(); request = request.clone({ setHeaders: { //'Content-Type': 'application/json; charset=utf-8', 'Authorization': `Bearer ${token}` } }); } return next.handle(request); }
class CustAdaptor extends UrlAdaptor { constructor(private baseurl: string) { super(); } beforeSend(dm: DataManager, request: XMLHttpRequest) { let url = this.baseurl + dm.dataSource.url; dm.dataSource.url = url; //settings.setRequestHeader("Hallo", "lalalalla"); } }If that works, that would be workaround until we can use httprequest interceptors.
|
@Component({
selector: 'app-root',
template:` <ejs-grid [dataSource]='data'>
. . .
</ejs-grid>`,
})
export class AppComponent implements OnInit {
. . .
@ViewChild('grid')
public grid: GridComponent;
ngOnInit(): void {
this.baseUrl = window.location.rel='nofollow' href;
this.pData = new DataManager({
url: this.baseUrl + 'Home/UrlDataSource', // customized URL
adaptor: new UrlAdaptor,
headers: [{ 'Authenticcation': 'bearer123' }] //set the header
});
}
} |
|
export class CustomAdaptor extends UrlAdaptor {
beforeSend(dm: DataManager, request: XMLHttpRequest) {
request.open('POST', 'UrlDatasource'); // customizing the Url of lHttpRequest
request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
dm.dataSource.headers = [{ 'Authorization': 'bearertoken' }]; // setting header
}
} |
|
@Component({
selector: 'fetchdata',
template: ' <ejs-grid #grid [dataSource]='pData' height='265px' [childGrid]='childGrid' [editSettings]='editSettings' [toolbar]='toolbar'>
<e-columns>
. . .
</e-columns>
</ejs-grid>',
encapsulation: ViewEncapsulation.Emulated
})
export class FetchDataComponent {
@ViewChild('grid')
public grid: GridComponent;
ngOnInit(): void {
this.pData = new DataManager({
url: 'Home/EmployeeDatasource',
adaptor: new CustomAdaptor,
});
. . .
}
}
|
Hi Marco,
You can achieve your requirement by using beforeSend() method of CustomAdaptor which is extended from the default UrlAdaptor. In that method you can change the url property of XmlHttpRequest by using open() method and set the header by using dataMaanger.headers property. We have prepared a simple sample based on your query. Please refer to the below code example and sample link.
[adaptor.ts]
export class CustomAdaptor extends UrlAdaptor {beforeSend(dm: DataManager, request: XMLHttpRequest) {request.open('POST', 'UrlDatasource'); // customizing the Url of lHttpRequestrequest.setRequestHeader('Content-Type', 'application/json; charset=utf-8');dm.dataSource.headers = [{ 'Authorization': 'bearertoken' }]; // setting header}}
[component.ts]
@Component({selector: 'fetchdata',template: ' <ejs-grid #grid [dataSource]='pData' height='265px' [childGrid]='childGrid' [editSettings]='editSettings' [toolbar]='toolbar'><e-columns>. . .</e-columns></ejs-grid>',encapsulation: ViewEncapsulation.Emulated})export class FetchDataComponent {@ViewChild('grid')public grid: GridComponent;ngOnInit(): void {this.pData = new DataManager({url: 'Home/EmployeeDatasource',adaptor: new CustomAdaptor,});. . .}}
Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/DataManager-xml-1000686029
Regards,Pavithra S.
Hi
I am using react with msal and i have similar scenario and beforesend is working fine. But when token expires, i need to fetch new token and if that fails too, i need to logout. How to achieve this? And also will this beforesend wait till new token is fetched. Please help me on these.
Hi Satheesh Kumar,
Greetings from Syncfusion support,
Based on your query when token expires, need to fetch new token and also will this beforesend wait till new token is fetched. In response to your query, we would like to clarify that our current architecture utilizes synchronous methods for "processQuery" and "beforeSend." Unfortunately, this means that it is not feasible for these methods to wait for the generation of a new token through an asynchronous process.
Please let us know if you need any further assistance.
Regards,
Vikram S