Please, could you help?
I have the following problem.
I want to use Upload file.
POST v1/files
I tested it on Postman and it works correctly.
But on the C# endpoint it doesn’t work and displays the Bad Request error.
Below is the source code.
ep.MapPost("/file", async (IFormFile file) =>
{
using (HttpClient client = new HttpClient())
using (Stream fileStream = file.OpenReadStream())
using (StreamContent fileContent = new StreamContent(fileStream))
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", chatGptApiKey);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
Dictionary<string, string> postParameters = new Dictionary<string, string>();
postParameters.Add("purpose", "assistants");
var formContent = new FormUrlEncodedContent(postParameters);
content.Add(formContent);
content.Add(fileContent);
var response = client.PostAsync(chatGptUrl + "/files", content).Result;
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadFromJsonAsync<ChatGPTFile>();
return TypedResults.Ok(responseString);
}
else
{
return Results.BadRequest("Erro ao chamar a API");
}
}
})
Tanks.