【问题标题】:Angular HTTPClient ignores headersAngular HTTPClient 忽略标头
【发布时间】:2019-03-12 10:40:19
【问题描述】:

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

(下面是我正在做的精简版)

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 属性/映射中。

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

经过反复检查,请求的所有内容都是正确的,标头在 Object 中(尽管在该 lazyUpdate 映射中),但由于某些未知原因,它们似乎在请求中被忽略了。因此,请求将其 Content-Type 属性覆盖,因为它认为那里没有。

有什么想法吗?

【问题讨论】:

    标签: angular angular-httpclient angular-http-interceptors


    【解决方案1】:

    似乎我一直在处理的文档是错误的。它与this.httpClient.put(url, body, options) 方法有关......似乎我正在努力理解body 参数是可选的,但事实并非如此。

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

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

    【讨论】:

    • 典型的橡皮鸭理论,花了很长时间试图修复它,在 StackOverflow 上解释它并在稍后解决它.....@_@
    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2021-02-25
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多