简体   繁体   English

无法从 Angular HTTP 拦截器获取请求标头值

[英]Can't get request headers values from Angular HTTP interceptor

In an Angular application, I use an interceptor to inject the token (when needed) into the request headers.在 Angular 应用程序中,我使用拦截器将令牌(在需要时)注入到请求标头中。 I also need it to modify the Content-Type header to the appropriate one.我还需要它将Content-Type标头修改为适当的标头。 By default, it must be application/json , but there is a call that needs to upload a ZIP file, so I want to let the Content-Type header as is (for these cases, it's something like: Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvamnUWepE840OEuq ).默认情况下,它必须是application/json ,但是有一个调用需要上传一个 ZIP 文件,所以我想让Content-Type标头保持原样(对于这些情况,它类似于: Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvamnUWepE840OEuq )。 I use this code:我使用这个代码:

    intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let usr = this.userService.getActualUser();
        let confirmationToken = '';
        let credentials = false;
        let contentType = 'application/json';
        
        // Check if the incoming request has an specific content type header
        if (req.headers.has('Content-Type')) {
          contentType = req.headers.get('Content-Type');
        }
    
        // ETC
    }

The problem is that req.headers.has('Content-Type') always returns false , so I'm setting the wrong type and the call fails.问题是req.headers.has('Content-Type')总是返回false ,所以我设置了错误的类型并且调用失败。 What am I doing wrong?我究竟做错了什么?

Thanks in advance!提前致谢!

I believe the content of your form is an instance of the FormData object?我相信您的表单内容是 FormData 对象的一个​​实例? If so,如果是这样的话,

if(req.body instanceof FormData)

This check might help you or give you some hints if you are unable to access the headers.如果您无法访问标题,此检查可能会帮助您或给您一些提示。 Or maybe you could even dive deeper and check into the req.body object to do a check that is more specific.或者,您甚至可以更深入地检查req.body对象以进行更具体的检查。

During your request, you would expect to see the content-type but you won't unless you directly specify this.在您的请求期间,您希望看到content-type但除非您直接指定它,否则您不会看到。

If you have this on your request, you will see the content type.如果您有此要求,您将看到内容类型。

return this.http.get(this.ENDPOINT, {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
})

This, however, could be an overkill.然而,这可能是一种矫枉过正。

Another possible solution is to check the instance type of the body and then set the headers accordingly另一种可能的解决方案是检查正文的实例类型,然后相应地设置标题

if (req.body instanceof FormData) {
    contentType = 'multipart/form-data';
}

Following your advices, I came up with a very simple solution, that is leave the Content-Type header as is if the request body is form data, and set is as JSON for the rest.按照您的建议,我想出了一个非常简单的解决方案,即保留Content-Type标头,就好像请求正文是表单数据一样,其余的设置为 JSON。 Something like:就像是:

    if(!(req.body instanceof FormData)) {
      contentType = 'application/json';
    }

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM