I need to send an HTML string to the backend to be processed. To do this I am using Angular's HttpClient
. However, the framework refuses to give me any meaningful error message other than a 400 response code. Below is my code.
Service method:
exportTestAsPdf(testHtml: string): Observable<any> {
return this.http.post<any>(this.baseTestApiUrl "Print", `\"${testHtml}\"`, this.requestHeaders);
}
requestHeaders:
protected get requestHeaders(): { headers: HttpHeaders | { [header: string]: string | string[]; } } {
const headers = new HttpHeaders({
Authorization: `Bearer ${this.authService.accessToken}`,
'Content-Type': "application/json",
Accept: "application/json, text/plain, */*"
});
return { headers };
}
ASP.NET endpoint:
[HttpPost]
public IActionResult Print([FromBody] string testHtml)
{
// Code here
}
It doesn't give me 404 so I know the endpoint is found. What is the problem?
I followed the answers in this SO post but those did not help me.
CodePudding user response:
Maybe use "${encodeURIComponent(testHtml)}" when posting your string
CodePudding user response:
Try to send the value like this.
exportTestAsPdf(testHtml: string): Observable<any> {
const body = { data : "Print" `\"${testHtml}\"`};
return this.http.post<any>(this.baseTestApiUrl , body, {this.requestHeaders});
}
In your backend, declare string data type named data.