azureazure-aivision-api

facing issues in executing Azure vision api


Hello All I am using Azure's vision analyze api to extract text from my documents,

here is the example code for your reference

//My main function fi.fullfile is the path of my uploaded document

AzureAnalyzeRequest(System.IO.File.ReadAllBytes(fi.FullName));

analyze function

static async void AzureAnalyzeRequest(byte[] byteData)
{
    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // Request headers
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "MyKey");

    // Request parameters
    queryString["language"] = "en";
    queryString["pages"] = "1,2";
    var uri = "https://url-ocr.cognitiveservices.azure.com/vision/v3.2/read/analyze?" + queryString;
    HttpResponseMessage response;
    using (var content = new ByteArrayContent(byteData))
    {
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        response = await client.PostAsync(uri, content);
    }
}

when the above function executed I am getting the error of 400 bad request

but when I tested my api on the below URL

Azure Vision api

it worked fine.

what I am doing wrong here?


Solution

  • {
        "url":""
    }
    
     public class Poco
        {
            public string url { get; set; }
        }
    
     Poco p = new Poco();
     p.url = "<URL OF YOUR IMAGE>";
     string json = JsonConvert.SerializeObject(p);
       
      // Here we are converting the json string to stringcontent which we can pass to httpclient  
           
     StringContent data = new StringContent(json, Encoding.UTF8, "application/json");
    

    Now all you have to do is call the Api:

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<YOURKEY>");
               
    var response = client.PostAsync(url, data);
    
    Console.WriteLine(response.Result.StatusCode);
    Console.WriteLine(response.Result);
    

    enter image description here