angular - 在 Angular 6 中添加 http 标头
问题描述
谁能告诉我这是否是在 Angular 6 中向 http 请求添加标头的正确方法?
当我通过 SwaggerUI 拨打电话时,我可以看到标题应该是:
url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff'
所以我添加了以下内容:
let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('HttpHeader1', 'Accept:application/json');
headers = headers.append('HttpHeader2', 'zumo-api-version:2.0.0');
然后调用:
getStuff(){
return this.http.get('https://myurl/tables/stuff', {headers})
}
没有失败但没有返回,我知道应该有。
谢谢
更新
刚刚注意到我通话中的网址实际上是 https 而不是 http,这有什么不同吗?
getStuff(){
return this.https.get('https://myurl/tables/stuff', {headers})
}
解决方案
正确的设置方法headers
是
let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('zumo-api-version', '2.0.0');
推荐阅读
- c# - 调用 List.Clear() 后,List 中的 List 返回 Empty
- python - 使用 Pathlib 检查两条路径是否指向同一事物,即使目标不存在
- java - 如何在 JFrame 中插入外键列
- python - Python - 展平嵌套的 JSON 文件
- javascript - 选择特定的映射项目 - 反应
- flutter - Flutter 的绝对位置
- postgresql - 输入几何具有 unkown(0) 几何(尽管使用了 st_transform)
- reactjs - 使用 React 的 Jimp.loadFont(使用 load-bmfont 模块)出错
- reactjs - Nginx 无法为 React 构建提供服务
- azure-data-explorer - Kusto avgif() greater than