15
const headers = new HttpHeaders({ 'Content-Type': 'text/xml' });
headers.append('Accept', 'text/xml');
headers.append('Content-Type', 'text/xml');
this.http.get('getxmlurl', {headers: headers}).subscribe(response => {
  return '1234';
});

Hi I am using angular 4 httpclient to make a http get request from a spring controller which returns a XML response.

The problem I have is the response is ALWAYS NULL even though I can see the xml response from the chrome developer network tab.

I thought it might be something to do with the request header, angular 4 defaults to json however I am unable to change the request header with the code above. Can someone please advice.

Thanks

Coderer
  • 25,844
  • 28
  • 99
  • 154
user2106630
  • 151
  • 1
  • 1
  • 4

3 Answers3

40

Set the responseType to text:

this.http.get('getXmlUrl', { responseType: 'text' }).subscribe(response => {
  console.log(response);
});

Allowed values for responseType:

  • arraybuffer
  • blob
  • json (default)
  • text

The responseType value determines how a successful response body will be parsed.

See Angular Docs:
HttpRequest # responseType
HttpClient # request()

Martin Schneider
  • 14,263
  • 7
  • 55
  • 58
  • for Observable it may also be a requirement to set observe: 'body' ... this.myHttpCliVar.get('pathToMyTargetUrl' , { observe: 'body', responseType: 'text'} ).subscribe(valAsStr => {this.localStrVar = valAsStr; }); notice that it is .get(...) not get(...) – Sql Surfer Jul 17 '19 at 22:03
  • On angular 8 don't forget cast as object `{ responseType: 'text' } ` – ontananza Jul 31 '19 at 20:20
  • "If `responseType` is the default `json`, you can pass a type interface for the resulting object as a type parameter to the call.". See also [Overload #3 of the `get()` method in docs](https://angular.io/api/common/http/HttpClient#get): The return value of `responseType: 'text'` is `Observable`. – Martin Schneider Aug 01 '19 at 08:11
8

2019 Angular 7 above HttpClient Note with Code

Get Angular Response as Text or Xml NOT as Json

Some subtle changes may have occurred in Angular 7 after the other previous answers were written. This is like a detailed comment with code to MA-Maddin's answer.

@Injectable()
export class MyWidgetService 

   private myHttpClient: HttpClient;

   constructor(httpClient: HttpClient) {
      super(httpClient); // you MIGHT need this 

      this.myHttpClient = httpClient; 
      }


   getResAsObservableStr = () => { 

      // Override the JSON Default Behavior.
      // 3 special things for text from HttpClient 
      //    a: Calling the raw .get('url')  NOT get<string>('url') 
      //    b: override observe: 'body' 
      //    c: override responseType: 'text' 

      return this.myHttpClient.get('pathUrlForCall' 
                 , { observe: 'body', responseType: 'text'} );
   }


// In Calling Coponent 
export class MyWidgetComponent 
   valAsStr: string;  

  constructor(
    // more vars like Router...
    private myWidgetSvcRef: MyWidgetService) { }

  ngOnInit() {

    this.getMyStrValFromWeb();

  } // ngOnInit end

  getMyStrValFromWeb = () => {

    this.myWidgetSvcRef.getResAsObservableStr()
    .subscribe(valAsStr => {this.valAsStr = valAsStr; });  

  } // end getMyStrValFromWeb


// in your html template (one possible scenario)

    <someControl [(ngModel)]="valAsStr" > </someControl>
Sql Surfer
  • 1,344
  • 1
  • 10
  • 25
  • Thanks for the answer. Passing { observe: 'body', responseType: 'text'} as options served the purpose. Its working for me – Arun s Jan 20 '21 at 12:24
1

The issue here is the HttpHeaders are immutable in angular. So instead of setting the values, you should set when you create the object. Something like

const headers = new HttpHeaders({ 'Content-Type': 'text/xml' }).set('Accept', 'text/xml');

But you are setting only request headers. If you want your response to be text/xml.

this.http.get('getxmlurl', { headers: headers, responseType: text/xml }).subscribe(response => { return response; });

You can remove headers unless you want to set request headers.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Karthikeyan Mohan
  • 319
  • 2
  • 4
  • 11