Angular 4 httpclient xml response

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 get http request from spring controller which returns XML response.

The problem I am facing is ALWAYS NULL, although I can see the xml response from the chrome network tab.

I thought it might have something to do with the request header, angular 4 default for json, but I can't change the request header with the code above. Can someone please the advice.

thank

+6


source to share


3 answers


Install responseType

in text

:

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

      

Valid 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 ()

+19


source


The problem here is that HttpHeaders are immutable in angular. Thus, instead of setting values, you must set when you create the object. Something like

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

      

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



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

      

You can remove the headers if you don't want to set the request headers.

+1


source


2019 Angular 7 above HttpClient Note with code

Get angular response as text or Xml, not as Json

Some minor changes may have happened in Angular 7 after other previous answers were written. It looks like a detailed comment with MA-Maddin's answer code.

@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>

      

0


source







All Articles