1
votes

I'm trying the following code:

  let myHeader = new HttpHeaders();
    myHeader.append("Authorization", Token);
    return this.http
      .get(this.service.GetDomainPrefix() + "Source/Download/" + id, 
        {       
          headers: myHeader, responseType: ResponseContentType.Blob
    }).map(res => {
    return {
      filename: name + '.mp4',
      data: res.blob()
    };
  })

but getting the following error:

Argument of type '{headers: HttpHeaders; responseType: ResponseContentType.Blobl;}' is not assignable to parameter of type 'RequestOptionsArgs'. Type 'HttpHeaders' is not assignable to type 'Headers'

The problems is only with the headers, because without it there are no errors. How can I add a head to my get request?

3
Is this for adding a security token to every request? If so, wouldn't be better an interceptor to do that instead for adding the header in every place you have an http request? - lealceldeiro
Are you using http(@angular/http) or HttpClient(@angular/common/http) ? - eduPeeth

3 Answers

1
votes

It's probably because you are using HttpModule (angular 2,4) instead of the new HttpClientModule (angular 4,5,6) (and the Http class instead of HttpClient class)

Also, HttpHeaders are immutable. To use the new HttpClientModule, use this code

import {HttpClient, HttpHeaders} from '@angular/common/http';

constructor(private http: HttpClient){}
//...
let myHeader = new HttpHeaders().append("Authorization", Token);
    return this.http
      .get(this.service.GetDomainPrefix() + "Source/Download/" + id, 
        {       
          headers: myHeader, responseType: 'blob'
    })
.map(res =>  {
      filename: name + '.mp4',
      data: res
          })

And you need to modify your AppModule to import HttpClientModule instead of HttpClient

1
votes

// HttpClient(@angular/common/http)

let myHeader = new HttpHeaders();
    myHeader.append("Authorization", Token);
let options =  {       
          headers: myHeader, responseType: 'blob'
    }

// Http(@angular/http)

 let myHeader = new Headers();
    myHeader.append("Authorization", Token);

let options = new RequestOptions() {       
          headers: myHeader, responseType: ResponseContentType.Blob
    }

//Request

return this.http
      .get(this.service.GetDomainPrefix() + "Source/Download/" + id, options).map(res => {
    return {
      filename: name + '.mp4',
      data: res.blob()
    };
  })
0
votes

import {Headers} from 'angular2/http'; var headers = new Headers();

This is what you should use. It's written in the error in simple words i.e. not assignable to type HttpHeaders(). You must use Headers()