本文整理汇总了TypeScript中@angular/common/http.HttpRequest类的典型用法代码示例。如果您正苦于以下问题:TypeScript HttpRequest类的具体用法?TypeScript HttpRequest怎么用?TypeScript HttpRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpRequest类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: intercept
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (!this.auth) {
this.auth = this.injector.get(AuthService);
}
if (this.auth.isAuthenticated()) {
request = request.clone({
setHeaders: {
'X-Auth-Token': this.auth.getToken()
}
});
}
// Add CSRF token for the Play CSRF filter
const token = this.cookieService.get('PLAY_CSRF_TOKEN');
if (token) {
// Play looks for a token with the name Csrf-Token
// https://www.playframework.com/documentation/2.4.x/ScalaCsrf
request = request.clone({
setHeaders: {
'Csrf-Token': token
}
});
}
return next.handle(request);
}
示例2: intercept
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token: string = this._auth.token;
if (token) {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
ContentType: 'application/json; charset=utf-8'
}
});
} else {
req = req.clone({
setHeaders: {
ContentType: 'application/json; charset=utf-8'
}
});
}
return next.handle(req).pipe(
tap(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this._auth.collectFailedRequest(req);
// console.log(err);
}
}
}
)
);
}
示例3: setReq
setReq(req: HttpRequest<any>, options: DelonAuthConfig): HttpRequest<any> {
const token = options.token_send_template.replace(
/\$\{([\w]+)\}/g,
(_: string, g) => this.model[g],
);
switch (options.token_send_place) {
case 'header':
const obj = {};
obj[options.token_send_key] = token;
req = req.clone({
setHeaders: obj,
});
break;
case 'body':
const body = req.body || {};
body[options.token_send_key] = token;
req = req.clone({
body: body,
});
break;
case 'url':
req = req.clone({
params: req.params.append(options.token_send_key, token),
});
break;
}
return req;
}
示例4: intercept
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const loginService = this.injector.get(LoginService);
const authHeader = loginService.getAuthorizationHeader();
const isAuthHeaderNeeded = !(req.url.includes('/account') || req.url.includes('/authenticate'));
let authReq = null;
if (isAuthHeaderNeeded) {
if (loginService.isAuthenticated()) {
authReq = req.clone({headers: req.headers.set('Authorization', authHeader), responseType: 'text'})
} else {
console.log('JWT expired');
loginService.logout();
this.router.navigate(['']);
}
} else {
authReq = req.clone({responseType: 'text'});
}
return next.handle(authReq)
.map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
return event.clone({ body: JSON.parse(event.body), });
}
})
.catch((error: HttpErrorResponse) => {
const parsedError = Object.assign({}, error, {error: JSON.parse(error.error)});
return Observable.throw(new HttpErrorResponse(parsedError));
});
}
示例5: intercept
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (!req.headers.has("Content-Type")) {
req = req.clone({
headers: req.headers.set("Content-Type", "application/json")
});
}
req = req.clone({ headers: req.headers.set("Accept", "application/json") });
if (this.auth.getToken() !== null) {
req = req.clone({
headers: req.headers.set("x-access-token", this.auth.getToken())
});
}
return next.handle(req).do(evt => {
if (evt instanceof HttpResponse) {
console.log("---> status:", evt.status);
console.log("---> filter:", req.params.get("filter"));
}
});
}
示例6: intercept
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!req.headers.has('Content-Type')) {
req = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
}
req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
console.log(JSON.stringify(req.headers));
return next.handle(req);
}
示例7: intercept
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Clone the request to add the new header.
const token = this.token.getToken();
const tenantId = this.token.getTenantId();
let authReq = req.clone();
if (token) {
authReq = req.clone({headers: req.headers.set('Authorization', 'Bearer ' + token)});
}
if (tenantId) {
authReq = authReq.clone({headers: authReq.headers.set('X-TENANT-ID', tenantId)});
}
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
}
示例8: intercept
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token: string = localStorage.getItem('token');
if (token) {
// "Authorization : Bearer" format are most likely implementing OAuth 2.0 bearer tokens.
request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)});
}
if (!request.headers.has('Content-Type')) {
request = request.clone({headers: request.headers.set('Content-Type', 'application/json')});
}
request = request.clone({headers: request.headers.set('Accept', 'application/json')});
console.log('%cHttpConfigInterceptor HttpRequest', 'color:yellow', request);
// this deals with the response
return next.handle(request).pipe(map(this.onSuccess), catchError(this.onError));
}
示例9: intercept
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
// Check if there is a token saved in localStorage
// const token: string = localStorage.getItem(AppSettings.global.tokenName);
console.log('AuthInterceptor Invoked');
const token =
// tslint:disable-next-line:max-line-length
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YmY1OGQ3ZDhiNDgwNTFlZThhZWQzMDAiLCJlbWFpbCI6ImNvc3RhX0l2bzNAZ21haWwuY29tIiwiaWF0IjoxNTQ0NjM1ODM2LCJleHAiOjE1NTMyNzU4MzZ9._cYkDjYwwUzOvzqp0HS8R86IgyyMmRCAgGGH0JEW3Ho';
if (token === null) {
// Pass on the original request.
return next.handle(req);
} else {
const authHeader = 'Bearer ' + token;
// Cloning Each Incoming Request as Incoming request is Immutable
const authReq = req.clone({
headers: req.headers
.set('Authorization', authHeader)
.set('Content-Type', 'application/json')
});
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
}
}
示例10: switchMap
switchMap((token) => {
return next.handle(req.clone({
setHeaders: {
Authorization: token
}
}));
})