Facing issues in executing Azure vision API (Bad Request)

Niteen Jadhav 1 Reputation point
2023-01-09T12:29:17.133+00:00

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

https://uaenorth.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-2/operations/5d986960601faab4bf452005/console

it worked fine.

what I am doing wrong here?

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
397 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 53,721 Reputation points
    2023-01-10T18:28:46.243+00:00

    Hello @Niteen Jadhav

    Just want to add more details to your question. I can understand your subscription and your image are working well in Azure Computer Vision V3.2 console but not working on the code you mentioned above.

    I hope you can understand the console is more for testing and trying.

    Based on this code, you are working on Azure Computer Vision V3.2 Client Library.

    To use the client library, you need to install firstly -

    Using Visual Studio, create a new .NET Core application.

    Once you've created a new project, install the client library by right-clicking on the project solution in the Solution Explorer and selecting Manage NuGet Packages. In the package manager that opens select Browse, check Include prerelease, and search for Microsoft.Azure.CognitiveServices.Vision.ComputerVision. Select version 7.0.0, and then Install.

    More information you can refer to below link, you can do the install in CLI as well. https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=visual-studio&pivots=programming-language-csharp

    In case you want to use REST API, please see this quick guidance - https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=cli&pivots=programming-language-rest-api

    Please have a try and let know how is everything going now. I hope this helps.

    Regards, Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.