在 observable 的 angular 中传递 Headers

Pass Headers in angular http in observable

如何在 http post 请求中传递 headers。

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();
        }));
    }

试试这个方法

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);
         });
});

简化版:

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);