1

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

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.