如何在Angular 6中将自定义HTTP标头值映射到请求正文

问题描述

我在登录API调用添加一个自定义HTTP标头,我希望将该标头的值作为用户名传递给登录名。

这是我的实现方式,我无法实现此方法。有人可以帮我吗?

登录服务方法

login(data) {
        return new Promise((resolve,reject) => {
            this.apiService.postApi(this.appConfig.endpoints.getotp,data,new HttpHeaders().append('user',''))
                .then((resp: any) => {
                    if (resp) {
                        // console.log(resp)
                        if (resp.status === this.appConfig.status.success) {
                            this.cache.loggedIn = true;
                            this.cache.user.roleId = resp.roleId;
                            this.cache.user.userId = resp.userId;
                            this.cache.user.roleName = resp.roleName;
                            this.cache.user.userFullName = resp.name;
                            this.cache.user.fullName = resp.fullName;
                            this.cache.user.authorization = resp.token;
                            this.cache.user.idleLoginValidity = resp.startUserSession;
                            this.cache.set('user',this.cache.user);
                            this.startUserSession(resp.idleLoginValidity);
                            resolve(resp);
                        } else {
                            reject(resp.message);
                        }
                    } else {
                        reject(this.apiService.commonStrings.http_error);
                    }
                },error => {
                    // console.log('Error!' + JSON.stringify(error));
                });
        });
    }

PostAPI方法,这是我添加登录服务方法添加的标头的地方。

postApi(url,headers) {
        if (!headers) {
            headers = new HttpHeaders({ 'Content-Type': 'application/json' });
        } else if ( headers && !headers.get('Content-Type')) {
            // headers.append('Content-Type','application/json' );
            headers = new HttpHeaders({ 'Content-Type': 'application/json' });
        }
        data = this.appendCommonParameters(data);
        console.log(data);
        console.log(headers);
        return new Promise((resolve,reject) => {
            this.http.post(this.appConfig.host + url,JSON.stringify(data),{ headers: headers })
                .pipe(map(res => res as {}),catchError(this.handleError))
                .subscribe(res => {
                    resolve(res);
                },(error: any) => {
                    // console.log(error)
                    reject(error);
                });
        });
    }

login.component.ts 登录方法中,我需要将标头值(即'user',')传递给 uname

  login() {
    const promise = new Promise((resolve,reject) => {
      this.authService.generatetoken().then((resp: any) => {
        if (resp.status === 'Change Password') {
            this.showChangePassword = true;
            this.formSubmitted = false;
            this.alert.error(resp.errorDesc);
        } else {
          console.log(resp);
            let payload;
            if (resp) {
              payload = {
                          'loginToken': resp.token,'upwd': resp.pwd,'uname': '','hsalt': resp.hsalt
                        };
                        console.log(payload);
              this.callLogin(payload);
            }
        }
    },(error) => {
        this.alert.error('Please try after sometime');
        this.formSubmitted = false;
    });
    });
    return promise;
  }

有人可以帮助我解决此问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)