本文整理汇总了TypeScript中@angular/common/http.HttpHandler类的典型用法代码示例。如果您正苦于以下问题:TypeScript HttpHandler类的具体用法?TypeScript HttpHandler怎么用?TypeScript HttpHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpHandler类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: intercept
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.get('No-Auth') === 'True') {
return next.handle(req.clone());
}
if (localStorage.getItem('currentUser') != null) {
const clonedreq = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('currentUser'))
});
return next.handle(clonedreq)
.do(
succ => { },
err => {
if (err.status === 401) {
this.router.navigateByUrl('/login');
}
}
);
} else {
this.router.navigateByUrl('/login');
}
}
示例2: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}
return next.handle(request);
}
示例3: intercept
intercept(req : HttpRequest <any>, next : HttpHandler) : Observable <HttpEvent<any>> {
// Clone the request to add the new header
const token = new TokenStorage();
const tokenVal = token.getToken();
const clonedRequest = req.clone({
headers: req
.headers
.set('Authorization', tokenVal ? `Bearer ${ tokenVal}` : '')
});
// Pass the cloned request instead of the original request to the next handle
return next.handle(clonedRequest);
}
示例4: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
let currentUser = this.authenticationService.currentUserValue;
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}
return next.handle(request);
}
示例5: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Add content-type for POST, PUT, PATCH automatically
if (request.method === 'POST' || request.method === 'PUT' || request.method === 'PATCH') {
request = request.clone({
setHeaders: {
'Content-Type': 'application/json; charset=utf-8'
}
});
}
return next
.handle(request)
.catch(response => {
let message: string;
if (response instanceof HttpErrorResponse) {
switch (response.status) {
case 0: {
message = "No network connection";
break;
}
case 401: {
message = "Unauthenticated";
break;
}
case 403: {
message = "Unauthorized";
break;
}
case 404: {
message = "Resource Not Found";
break;
}
case 500: {
message = "Server error";
break;
}
default: {
message = response.status.toString();
break;
}
}
}
this.snackBar.open(message, null, {
duration: 2000
});
return Observable.throw(response);
});
}
示例6: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
headers: new HttpHeaders(
{
'Content-Type': 'application/json',
'Authorization': 'desafio-4-devs'
}
)
});
return next.handle(request);
}
示例7: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do(
(event: HttpEvent<any>) => {},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (!(err.status === 401 && (err.message === '' || (err.url && err.url.indexOf('/api/account') === 0)))) {
if (this.eventManager !== undefined) {
this.eventManager.broadcast({ name: 'jHipsterRegistryApp.httpError', content: err });
}
}
}
}
);
}
示例8: intercept
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const user: User = this.sessionStorage.retrieve('user');
if (user && user.token) {
request = request.clone({
setHeaders: {
JWT: user.token,
},
});
}
return next.handle(request);
}
示例9: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(
(event: HttpEvent<any>) => {},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.loginService.logout();
}
}
}
)
);
}
示例10: intercept
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(req.method);
if (req.method == 'OPTIONS') {
console.log("OPTIONS!");
}
console.log(req.body);
/* if (req.url.startsWith("http://localhost:8080/")) {
const newBody = this.toJsonString(req.body);
const modified = req.clone({body: newBody});
return next.handle(modified);
} */
console.log(req.url);
return next.handle(req);
}