小编39r*_*9ro的帖子

Interceptor Angular 4.3 - 在克隆的请求上设置多个标头

我刚刚注意到新Interceptor中不再支持可能在先前的HTTP RequestsOption中使用的Header对象.

这是新的拦截器逻辑:

// Get the auth header from the service.
const authHeader = this.auth.getAuthorizationHeader();
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
Run Code Online (Sandbox Code Playgroud)

我现在有两种方法可以在此请求中添加标题:

例:

headers?: HttpHeaders;

    headers: req.headers.set('token1', 'asd')
Run Code Online (Sandbox Code Playgroud)
setHeaders?: {
   [name: string]: string | string[];
};

    setHeaders: {
             'token1': 'asd',
             'token2': 'lol'
    }
Run Code Online (Sandbox Code Playgroud)

如何在此请求中有条件地添加多个标头?与我以前使用Header对象相同:

 myLovellyHeaders(headers: Headers) {
    headers.set('token1', 'asd');
    headers.set('token2', 'lol');
     if (localStorage.getItem('token1')) {
     headers.set('token3', 'gosh');
     }
    }
    const headers = new Headers();
    this.myLovellyHeaders(headers);
Run Code Online (Sandbox Code Playgroud)

header interceptor angular

22
推荐指数
4
解决办法
3万
查看次数

如何在 PageSpeed Insights 上测试受保护的 URL(身份验证)?

我希望能够使用 PageSpeedInsights测试需要用户名密码的受保护 URL 。

我尝试使用http(s)://user:password@server但它看起来不起作用!

是否可以?任何的想法?
谢谢!

authentication pagespeed

5
推荐指数
0
解决办法
1171
查看次数

标签 统计

angular ×1

authentication ×1

header ×1

interceptor ×1

pagespeed ×1