I wanna get a pdf file from API in ASP.Net core, but I don't know how to do this. what I do is send a model to the api and I receive a pdf file
This is the method in my API that returns the file (In Postman it works)
[HttpPost]
public ActionResult getPDF([FromBody] Models.Request.InvoiceRequest model) {
if (Verify(model.Token)) // This is a method that validate access token
{
byte[] file = pdfConf(model); // This is a method that converts html file to pdf
return File(file, "application/pdf"); // This returns the pdf file
}
return Ok("Invalid action");
}
This is my code in the web application, but it doesn't work.
[HttpGet]
public FileResult getPDF(int factura)
{
this.GetSession();
// Object sent to the web api
FacturaRequest oFacturaRequest = new FacturaRequest();
oFacturaRequest.Token = oUserSession.accessToken;
oFacturaRequest.IdFactura = factura;
RequestUtil oRequestUtil = new RequestUtil();
JavaScriptSerializer js = new JavaScriptSerializer();
string json = JsonConvert.SerializeObject(oFacturaRequest); // Convert model to json format
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.Url.GETFACTURA);
request.Method = "POST";
request.PreAuthenticate = false;
request.Accept = "application/json";
request.MediaType = "application/json";
request.ContentType = "application/json; charset=utf-8";
request.Timeout = 60000;
using (var oStreamWriter = new StreamWriter(request.GetRequestStream()))
{
oStreamWriter.Write(json);
oStreamWriter.Flush();
}
HttpWebResponse oHttpResponse = (HttpWebResponse)request.GetResponse();
var result = "";
using (var oStreamReader = new StreamReader(oHttpResponse.GetResponseStream()))
{
result = oStreamReader.ReadToEnd();
}
return File(result,"application/pdf","Factura");
}