【问题标题】:Angular httpclient overriding content-type header [duplicate]Angular httpclient覆盖内容类型标头[重复]
【发布时间】:2021-08-25 13:59:51
【问题描述】:

我正在尝试使用 Angular httpClient 将 ndjson 块发送到 API。我要联系的端点不接受对象数组,并且每个 JSON 对象都必须用换行符来描述。因此,我不能只使用典型的 JSON 对象,而是使用一串 JSON 对象,每个对象后面都有一个换行符。然后我尝试将 Content-Type 标头设置为 applicaiton/json,因为 API 仍然需要它,但是当请求发出时,httpClient 会覆盖我的标头并将其设置回 text/plain。

如何防止这种覆盖发生?

代码:

bulkImport(importData: string, indexName: string){
  const headers = new HttpHeaders();
  headers.set('Content-Type', 'application/json; charset=utf-8');
  return this.http.put<any>(this._baseUrl + indexName + "/_bulk", importData, {headers: headers});
}

【问题讨论】:

  • 试试:const headers = { 'Content-Type': 'application/json'};
  • 谢谢!那行得通

标签: angular angular-httpclient


【解决方案1】:

感谢 Najam Us Saqib 的评论!这有效:

const headers = { 'Content-Type': 'application/json'};

【讨论】:

  • 检查重复的问题并以正确的方式使用 HttpHeaders,而不是这种解决方法 ;) 也让你明白为什么你的第一次尝试没有奏效:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 2015-12-22
  • 2021-05-03
  • 2017-04-27
  • 2018-05-28
相关资源
最近更新 更多