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);
});
}
CodePudding user response:
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