【问题标题】:Adding http headers in Angular 6在 Angular 6 中添加 http 标头
【发布时间】:2019-01-30 01:12:46
【问题描述】:

谁能告诉我这是否是在 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})
  }

没有失败但没有返回,我知道应该有。

谢谢

更新

刚刚注意到我调用的 url 实际上是 https 而不是 http,这有什么不同吗?

getStuff(){
        return this.https.get('https://myurl/tables/stuff', {headers})
      }

【问题讨论】:

  • 你得到HttpHeader1 的地方实际上应该是标题名称,即Accept,你得到Accept:application/json 的地方应该是值,即application/json,所以你其实想要的是headers.append('Accept', 'application/json');
  • 确保在 HTTP 调用中也调用 subscribe。 Observables 是惰性的,所以它只会在订阅了某些东西后才进行 HTTP 调用

标签: angular http visual-studio-code http-headers


【解决方案1】:

角度6+

申报区:

httpOptionsNoAuth : any;

初始化:

constructor(){
    this.httpOptionsNoAuth = {
        headers: new HttpHeaders().set('No-Auth', 'true')
    };
}

用法:

return this._http.get<any>(`${url}`, { headers: this.httpOptionsNoAuth.headers});

【讨论】:

    【解决方案2】:

    我在我的代码中已经这样做了

    httpOptions={ headers: new HttpHeaders({ 'Content-Type': 'application/json'})};
     this.httpOptions.headers = this.httpOptions.headers.append('Token', this.Token);
    

    然后在我的 http.get 调用中,我这样做了:

    return this.http.get<JSON>(this.API_ADDRESS+'/api/RemoveEmployee/'+id,this.httpOptions
    

    【讨论】:

      【解决方案3】:

      试试下面可能对你有帮助的代码。

      let headers = new HttpHeaders().set('Accept': 'application/json').set('zumo-api-version': '2.0.0')
      

      【讨论】:

      • 在尝试帮助他人之前,至少要学会格式化自己的代码。
      • @JhourladEstrella 谢谢你的建议。下次我会照顾的。
      • 投反对票,因为添加标头的语法不准确。正确的方法是append,而不是set。
      • 我为 Angular 6 提供这个。[angular.io/api/common/http/HttpHeaders]
      【解决方案4】:

      您没有得到任何回报,因为您没有订阅该活动。将.subcribe 添加到您调用它的函数中例如

      getStuff().subscribe(data=>{ console.log(data); } )

      因此,您订阅的 data 包含所有响应以及您需要了解的有关该呼叫的所有信息。

      您可以从这里阅读更多内容https://angular.io/guide/http

      【讨论】:

        【解决方案5】:

        Angular 6 格式:

        let headers = new HttpHeaders({
            'Accept': 'application/json',
            'zumo-api-version': '2.0.0'
        });
        

        【讨论】:

        • 尝试将 'Accept': 'application/json' 更改为 'Content-Type': 'application/json'
        • 设置标题的简单方法:)
        【解决方案6】:

        设置标题的正确格式如下所示。

        let headers: HttpHeaders = new HttpHeaders();
        headers = headers.append('Accept', 'application/json');
        headers = headers.append('zumo-api-version', '2.0.0');
        

        url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff'

        在上述请求中,标头键的名称是 Accept & zumo-api-version ,之前的文本:
        标头基本上设置为键/值对

        【讨论】:

        • 让 options = new RequestOptions({ headers: headers }); return this._http .get(this._url,options) 确保您使用上述语句调用获取请求,方法是在请求调用中传递标头
        • 同样的事情 - 没有数据,是的,我大摇大摆地返回了它。不过,我想我错过了订阅,我会在早上,现在回家的时间进行测试。
        • 如果是GET请求,就没有内容,所以Content-Type无所谓
        【解决方案7】:

        设置headers的正确方法是

        let headers: HttpHeaders = new HttpHeaders();
        headers = headers.append('Accept', 'application/json');
        headers = headers.append('zumo-api-version', '2.0.0');
        

        【讨论】:

        • 谢谢,那么调用应该还是:getStuff(){ return this.http.get('myurl/tables/stuff', {headers}) } ?
        • 先生,我尝试了你的代码,但我得到了 ERROR TypeError: CreateListFromArrayLike called on non-object in the console 你能告诉我如何解决吗?
        猜你喜欢
        • 1970-01-01
        • 2018-11-03
        • 2019-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多