1

I'm trying to call this API from my C# app: https://ocr.space/OCRAPI

When I call it from curl, it just works fine:

curl -k --form "file=@filename.jpg" --form "apikey=helloworld" --form "language=eng" https://api.ocr.space/Parse/Image

I implemented it this way:

 [TestMethod]
    public async Task  Test_Curl_Call()
    {

        var client = new HttpClient();

        String cur_dir = Directory.GetCurrentDirectory();

        // Create the HttpContent for the form to be posted.
        var requestContent = new FormUrlEncodedContent(new[] {
              new KeyValuePair<string, string>(  "file", "@filename.jpg"), //I also tried "filename.jpg"
               new KeyValuePair<string, string>(     "apikey", "helloworld" ),
        new KeyValuePair<string, string>( "language", "eng")});

        // Get the response.
        HttpResponseMessage response = await client.PostAsync(
           "https://api.ocr.space/Parse/Image",
            requestContent);

        // Get the response content.
        HttpContent responseContent = response.Content;

        // Get the stream of the content.
        using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
        {
            // Write the output.
            String result = await reader.ReadToEndAsync();
            Console.WriteLine(result);
        }

    }

I get this answer :

{
    "ParsedResults":null,
    "OCRExitCode":99,
    "IsErroredOnProcessing":true,
    "ErrorMessage":"No file uploaded or URL provided",
    "ErrorDetails":"",
    "ProcessingTimeInMilliseconds":"0"
}

Any clue?

What's the @ character for in "file=@filename.jpg"?

I put my filename.jpg file in the project and test project bin/debug directory and run my test project in debug mode.

So I don't think the error points to the file not being where expected. I'd rather suspect a syntax error in my code.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
A D
  • 307
  • 3
  • 21

1 Answers1

2

The error message is telling you what's wrong:

No file uploaded or URL provided

You sent a filename to the service in your code, but that's not the same thing as giving curl a filename. curl is smart enough to read the file and upload the contents with your request, but in your C# code, you'll have to do that yourself. The steps will be:

  1. Read the file bytes from disk.
  2. Create a multipart request with two parts: the API key ("helloworld"), and the file bytes.
  3. POST this request to the API.

Fortunately, it's pretty easy. This question demonstrates the syntax to set up a multipart request.

This code worked for me:

public async Task<string> TestOcrAsync(string filePath)
{
    // Read the file bytes
    var fileBytes = File.ReadAllBytes(filePath);
    var fileName = Path.GetFileName(filePath);

    // Set up the multipart request
    var requestContent = new MultipartFormDataContent();

    // Add the demo API key ("helloworld")
    requestContent.Add(new StringContent("helloworld"), "apikey");

    // Add the file content
    var imageContent = new ByteArrayContent(fileBytes);
    imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
    requestContent.Add(imageContent, "file", fileName); 

    // POST to the API
    var client = new HttpClient();
    var response = await client.PostAsync("https://api.ocr.space/parse/image", requestContent);

    return await response.Content.ReadAsStringAsync();
}
Community
  • 1
  • 1
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147