I am currently making an API call where I am trying to attachment a file from my system to trello using the API, if i go via postman and attachment my file using form data in the body it attaches successfully, but when I try to do it using my code it gives me a 400 Bad request. Here is my endpoint:
public async Task<IActionResult> AddCard([FromForm] IFormFile file)
{
if (file == null || file.Length == 0)
{
return BadRequest();
}
CardModel result = await _trelloRepository.AddAttachment(file);
return Ok(result);
}
and this is my service where the error is occuring:
public async Task<CardModel> AddAttachment(IFormFile file)
{
HttpClient client = new HttpClient();
try
{
string AddCard = String.Format("https://api.trello.com/1/cards/{0}/attachments?key={1}&token={2}", "610c13f817940f7daf32dc2a", key, token);
var myContent = System.Text.Json.JsonSerializer.Serialize(file);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage httpResponse = await client.PostAsync(AddCard, byteContent);
string content = await httpResponse.Content.ReadAsStringAsync();
return content;
}
I noticed when using the trello api in postman I had to name the form data request as file because when i named it something like image, it did not work