可观察到的角度HTTP中传递标题

时间:2018-12-03 11:46:29

标签: angular http angular7

如何在http发布请求中传递标题。

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 个答案:

答案 0 :(得分:2)

简化版本:

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

答案 1 :(得分:1)

尝试这种方式

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