Enzo B.
Enzo B.

Reputation: 2371

C# POST JSON Request to Google Vision

Being locked on .NET 2.0, i can't use the GOOGLE VISION C# API which is only available since.NET 4.0.

So I wanted to use this API with web request like this :

string webAddr = "https://vision.googleapis.com/v1/images:annotate?key=XXXXXXXXXXX";

var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = " { \"requests\": [ { \"image\": { \"content\": " + this.b64 + " }, \"features\": [ {\"type\": \"TEXT_DETECTION\" }] }] }";
    
    streamWriter.Write(json);
    streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var responseText = streamReader.ReadToEnd();
    Console.WriteLine(responseText); 
}

The problem is that I permanently have a 400 return from google and after a lot of searching I can't find a solution.

How can I solve this problem?

Upvotes: 4

Views: 1796

Answers (1)

Max Bender
Max Bender

Reputation: 392

You may use HTTP POST requests or Cloud Vision API Client Libraries to interact with requests and responses from this API.

Using HTTP Protocol exchange method allows you:

  • not to depend from the library version and the Framework version
  • use any way of HTTP request creation (HTTPClient or HTTPWebResponse or another)
  • you receieve the same result as while using Client Library
  • using Tasks allows you to perform async request in synchronous context

Simple Cloud Vision API usage for Text Detection may be performed in this way:

string base64string = "";
Bitmap bmpArea = null;

//preparing our image
using (MemoryStream m = new MemoryStream())
{
    bmpArea.Save(m, ImageFormat.Jpeg);
    byte[] imageBytes = m.ToArray();

    base64string = Convert.ToBase64String(imageBytes);
}
//call request asynchronically
Task.Run(async () => await Google_Vision_API_Request(base64string));

Request with JSON-content example:

public async Task Google_Vision_API_Request(string base64string)
{
    HttpResponseMessage response;
    using (var client = new HttpClient())
    {

        string myJson = $@"{{
                    ""requests"":[
                        {{
                            ""image"":{{
                                ""content"": ""{base64string}""
                            }},
                            ""features"":[
                                {{
                                    ""type"": ""TEXT_DETECTION""
                                }}
                            ]
                        }}
                    ]
                }}";

        string requestUri = "https://vision.googleapis.com/v1/images:annotate?key=";
        requestUri += google_api_key_var;
        response = await client.PostAsync(
            requestUri,
            new StringContent(myJson, Encoding.UTF8, "application/json"));

        responseStr = await response.Content.ReadAsStringAsync();
    }
}

Upvotes: 5

Related Questions