I created a document using the IText library and I want to send this document in a post request to my other API. I tried to turn the document into an array of bytes but was unsuccessful and I got the following error: HTTP 415 Unsupported Media Type. How can I send and receive this document?
How I am sending:
var url = @"https://localhost:7253/User/Test";
var httpClient = new HttpClient();
using (MemoryStream ms = new MemoryStream())
{
PdfWriter writer = new PdfWriter(ms);
PdfDocument pdf = new PdfDocument(writer);
MultipartFormDataContent form = new MultipartFormDataContent();
Document document = new Document(pdf);
Paragraph p = new Paragraph("Hello World!")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(24);
document.Add(p);
document.Close();
byte[] pdfBytes = ms.ToArray();
ByteArrayContent content = new ByteArrayContent(pdfBytes);
content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
form.Add(content, "pdf", "documento.pdf");
HttpResponseMessage response = await httpClient.PostAsync(url, form);
if (response.IsSuccessStatusCode)
{
}
}
How I'm getting:
[HttpPost("Teste")]
public IActionResult Post([FromBody] byte[] pdfBytes)
{
Document document = ByteArrayToDocument(pdfBytes);
return Ok();
}