How to call the OCR REST API in PowerShell

Can anyone see what is wrong here?

Invoke-RestMethod -Uri https://api.ocr.space/parse/image -Method Post -Headers @{"apikey"="xxxxxxxxxx"} -InFile "C:\Users\My\Path\To\test.pdf"

It returns error 99 which is an unknown error and the error message is {Parameter name '%PDF-1.4…

I’ve tried dozens of combinations and still get various errors. In the meantime I had no idea but curl is now built into W10 since last year so I’m making the call like this for uploading receipts for OCR:

curl.exe -X POST --header "apikey:xxxxxx" --form "file=@C:\Users\Path\Path\test.pdf" --form "isTable=true" --form "scale=true" https://api.ocr.space/Parse/Image | ConvertFrom-Json -OutVariable receipt

This gets the data down to just the receipt itself:

$receipt = $receipt | select -expand ParsedResults | select -expand ParsedText

All my tests with Powershell Invoke-RestMethod failed as well with error code 99 and “{Unable to recognize the file type}”. According to Stackoverflow and other websites, it seems Invoke-RestMethod has problems multi-part api calls. Somewhere else I read that Powershell 6 Core is required.

Anyway, my solution was just like yours, to use curl inside Powershell - this works great.

$result = Invoke-Expression e:\work\e2\curl.exe -H "apikey:XXXXX" https://api.ocr.space/parse/image --data "url=http://yyyyyy/zzzz.jpg&ocrengine=2"

Or to read from a file:

$result = Invoke-Expression e:\work\e2\curl.exe -H "apikey:XXXXX" --form "file=@d:\test\zzz.tif" --form "language=eng" --form "detectOrientation=true" https://api.ocr.space/parse/image

PS: If someone has a Powershell only solution, I would be curious how the solution looks like :wink:

So, it seems that Powershell Invoke-Expression command inherently does not support multipart file upload. You could use the ‘HttpClient’ for the same. But it is much easier to use cURL with Powershell. We created a code sample for this:

Please see Powershell OCR API code.

I forgot to come back and post but this is how to do the API call natively in PowerShell. This is preferred because all of the data is already returned in an object and less post processing would be needed as with cURL:

$Form = @{

            isTable = "true"

            scale   = "true"

            file    = Get-Item -Path "C:\Path\mydoc.pdf"

        }

        Invoke-RestMethod -Method Post -Uri "https://api.ocr.space/Parse/Image" -Headers @{apikey = "0000000"} -Form $Form

Adding Get-Item actually sends the file itself instead of the path text. This is the same as the @ in cURL which tells it to not send the text, but the file.
I’m also on PowerShell 7, not sure if there is additional code needed for older versions.

1 Like