Angular HTTPClient 忽略标头 [英] Angular HTTPClient ignores headers

查看:38
本文介绍了Angular HTTPClient 忽略标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,在发出请求时我的标头被忽略了.

For some reason my headers are being ignored when the request is made.

(以下是我正在做的修剪版本)

let headers = new Headers();

headers = headers.set('Content-Type', file.mime);
headers = headers.set('X-Blah', "Test");

return this.httpClient.put<HttpEvent<Blob>>(`${encodeURIComponent(file.name)}`, {
   data: file,
   responseType: "blob",
   observe: "events",
   headers: headers,
   withCredentials: this.configuration.withCredentials
});

我在使用开发工具时注意到 [HTTPHeaders] headers 对象只是将新的 header 放入 lazyUpdate 属性/地图.

What I notice when using the dev tool is that the [HTTPHeaders] headers object is only putting the new header into the lazyUpdate property/map.

当前有一个 HTTPInterceptor,当它触发时,请求对象不包含我在请求中设置的任何标头.

There is currently a HTTPInterceptor in place, which when fires, the request object contains none of the headers that I set in the request.

它已经被一遍又一遍地检查,请求的一切都是正确的,标头在对象中(尽管在那个 lazyUpdate 映射中),但是由于某些未知的原因,它们似乎在请求中被忽略了.因此,请求将其 Content-Type 属性覆盖,因为它认为那里没有.

It's been checked over and over and everything is correct for the request, the headers are in the Object (although in that lazyUpdate map), but for the some unknown reason they just seem to be ignored in the request. As such, the request is having it's Content-Type property overridden since it thinks there is none there.

有什么想法吗?

推荐答案

似乎我一直在处理的文档是错误的.它与 this.httpClient.put(url, body, options) 方法有关......似乎我正在理解 body 参数是可选的,它不是.

Seems that the documentation that I have been working on were wrong. It had to do with the this.httpClient.put(url, body, options) method....seems I was working on the understanding that the body parameter was optional, which it's not.

所以最后一次调用应该将 Blob 作为第二个参数传递,并将选项保留为第三个参数...

So the last call should have passed the Blob as the second parameter, and kept the options as the third parameter...

return this.httpClient.put<HttpEvent<Blob>>(`${encodeURIComponent(file.name)}`, file, {
   responseType: "blob",
   observe: "events",
   headers: headers,
   withCredentials: this.configuration.withCredentials
});

这篇关于Angular HTTPClient 忽略标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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