I need to send exactly PNG image to the server (by FormData). The request was sent successfully but the response indicated that "Wrong file format" (I don't have the right to access the server to check what is going on).
I'm currently using .NET 6
private async Task RequestApiRegister(string bookingId, string name)
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Config.TOKEN_REGISTER);
try
{
string aliasId = bookingId + "__" + name;
using FileStream fs = File.OpenRead(_filePath); // --> Absolute file path, ex: "E:\\Blabla\\IMG\\image_2023_03_24T19_54_30_9573981.png"
var formData = new MultipartFormDataContent();
formData.Add(new StringContent("secret"), "organization");
formData.Add(new StringContent(aliasId), "aliasId");
formData.Add(new StreamContent(fs), "images[]", Path.GetFileName(_filePath));
string url = "https://some-endpoint.com/register";
var response = await client.PostAsync(url, formData);
//response.EnsureSuccessStatusCode();
string sd = response.Content.ReadAsStringAsync().Result;
} catch (Exception e)
{
Console.WriteLine(e);
}
}
I have spent hours searching SO but haven't found any answers yet. Here is some I have tried.
How to upload file to server with HTTP POST multipart/form-data?
More but I don't remember, sorry