@vievillage Thanks for getting back and sharing updates about my action plan. Since all the action plans shared in my above comment works fine, we have isolated that the issue is specific to your application.
I have created a simple application which works fine. I am sharing the code here if that helps:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System.Net.Http.Headers;
namespace OpenAIWebAPi.Controllers
{
[Route("api/Home")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly string _subscriptionKey = "771f958cXXXXXXXXXXXXXXXXX6da83";
private readonly string _visionEndpoint = "https://VISIONNAME.cognitiveservices.azure.com/";
[HttpPost]
public async Task<IActionResult> Post(IFormFile file)
{
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _subscriptionKey);
byte[] data;
using (var br = new BinaryReader(file.OpenReadStream()))
data = br.ReadBytes((int)file.OpenReadStream().Length);
ByteArrayContent bytes = new ByteArrayContent(data);
bytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync(new Uri($"{_visionEndpoint}/computervision/imageanalysis:analyze?api-version=2023-02-01-preview&features=caption"), bytes);
response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
return BadRequest("Failed to analyze image. Please try again later.");
}
var responseJson = await response.Content.ReadAsStringAsync();
var captionResult = JsonConvert.DeserializeObject<dynamic>(responseJson).captionResult;
string captionText = null;
if (captionResult != null)
{
captionText = captionResult.text.ToString();
}
return Ok(captionText);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, $"Error in image analysis: {ex.Message}");
}
}
}
}
You can then leverage the postman tool with request header Content-Type : multipart/form-data to send the request to this application as shown below and you should see 200 success status code. :
More info about the vision API is here.
Hope this helps.
**
Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.