代码之家  ›  专栏  ›  技术社区  ›  kenny

在可观察的角HTTP中传递头

  •  1
  • kenny  · 技术社区  · 7 年前

    如何在HTTP POST请求中传递头。

    import { Http, Response } from '@angular/http';
    
    import { HttpHeaders } from '@angular/common/http';
    
    import { Observable } from 'rxjs';
    import { map, catchError } from 'rxjs/operators';
    
    getStreamingURL(): Observable<any> {
    		const headers: HttpHeaders = new HttpHeaders({
    			'deviceType': 'pc',
    			'os': 'web'
    		});
    
    		const options = {
    			'headers': headers
    		};
            return this.http.post(
                environment.apiUrl + '/livetv/apis/v1.0/stream',
                {
    				'channelId': 142
    			}, options
            ).pipe(map((res) => {
                return res.json();
            }));
        }
    2 回复  |  直到 7 年前
        1
  •  2
  •   Jacopo Sciampi    7 年前

    简化版:

    import { Http, Headers, RequestOptions, Response } from '@angular/http';
    
    const headers: HttpHeaders = new HttpHeaders({
                'deviceType': 'pc',
                'os': 'web'
            });
    
    const options = new RequestOptions({ headers: headers });
    
    return this.http.post(urlToPass, options);
    
        2
  •  1
  •   Sachin Shah    7 年前

    试试这个方法

    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Observable } from 'rxjs/Observable';
    
    // While call any API.
    
    return new Observable<any>(observer => {
    let headers = new HttpHeaders({
        'deviceType': 'pc',
        'os': 'web'
      });
      let options = {
        headers: headers
      }
    
     this.http.post(URL, param, options)
            .subscribe(data => {
                console.log(data);
             });
    });
    
    推荐文章