Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular – Return XML data from server

I am creating XML data on the server side and sending to my Angular client to save as xml file. But for some reason in the httpClient.post method I get error. Dont understand what is the reason. In the browser console I see the XML data in the Response but for some reason it is not printing the data. I will appreciate help to debug issue.Below is my client code.

Service class

public createXML(data:IJob)
{
    console.log("createXML - POST data to server: ", data);
    let headers = new HttpHeaders();
    headers = headers.append('Content-Type', 'application/xml');
    return this.httpClient.post<any>(environment.restUrl + "/createXML/",data)
    .pipe(
      tap((data) => console.log(data)),
      catchError(this.handleError)
    );
}

Component class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

createXML(data:IJob):void{
  this.coreService.createXML(data).subscribe(
    (data: Response) => {
      console.log('XML file',data);
      this.downloadFile(data.text(), 'lot', 'gtin');
    }, (err: any) => {
      console.log(err);
    });
  }

>Solution :

This is happening because the default type of your response is JSON, you need to set the response as text

In order to achieve this you have to:

public createXML(data:IJob)
{
    console.log("createXML - POST data to server: ", data);
    let headers = new HttpHeaders();
    headers = headers.append('Content-Type', 'application/xml');
    return this.httpClient.post<any>(
      environment.restUrl + "/createXML/",
      data, 
      {
        responseType: "text" // this is 
      }
     ).pipe(
      tap((data) => console.log(data)),
      catchError(this.handleError)
    );
}

For more information you see how to make a POST request

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading