访问我的Angular应用程序时如何获取请求标头值 [英] How to get Request Header value when accessing my Angular application

查看:98
本文介绍了访问我的Angular应用程序时如何获取请求标头值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将从另一个应用程序访问我的Angular应用程序,并以如下方式配置用于部署我们的应用程序的环境:该应用程序发出的访问我的Angular应用程序的请求将包含一些在请求"中设置的信息标头.我被要求使用标题信息来标识从中调用Angular应用程序的应用程序.另一个应用程序可以是外部部署的应用程序,也可以是内部部署的应用程序.该信息将在请求标头中提供.

My Angular application will be accessed from another application and the environment which is used to deploy our applications are configured in such a way that, the request coming from that application to access my Angular application will contain some information which is set in Request Header. I'm asked to use that header information to identify the application from which my Angular application is called. The other application can be either External or Internal deployed application. And that information will be available in the request header.

我无法找到有关如何在Angular中获取/读取我的应用程序访问请求的标头信息的任何方法.有人请帮助我.

I'm not able to find any way on how to get/read header information of the my application access request in Angular. Somebody please help me..

推荐答案

,因为没有方法可以做到这一点. 您的应用程序正在浏览器中运行,因此您的应用程序未收到任何请求,但浏览器却收到了.您可能应该找到其他架构.让您的后端参与流程.也许构造一个将重定向到您的应用程序的端点.

because there is no method to do this. Your Application is running in the browser, so your application doesnt receive any requests, but the browser does. You should probably find another architecture. Involve your backend in the flow. Maybe construct an endpoint which is redirecting to your application.

另一个相同的问题

所以我知道从标头中检索任何内容的唯一方法是httpInterceptor,它拦截任何传出的请求并观察响应,如下所示:

So the only way I know to retrieve anything from the headers is the httpInterceptor, that is intercepting any outgoing request and is observing the response like so:

import { Injectable } from '@angular/core';
import { HttpInterceptor, 
         HttpEvent, 
         HttpHandler, 
         HttpRequest, 
         HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class HeaderInterceptor implements HttpInterceptor {

   constructor(private userService: UserService, 
               private constantsService: ConstantsService){ }

public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    if(req.url.includes(/*whatever you are looking for*/)) { 

    }

    return next.handle(req).do((suc) => {
        if(suc.type !== 0){
            const whateverDataYouNeed = suc['headers'].get(/*whatever header you need*/);
            // Do something with it           
        }

    }).
    catch((error, caught) => {
        return Observable.throw(error);
    }) as any;
  }
}

最诚挚的问候!

这篇关于访问我的Angular应用程序时如何获取请求标头值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆