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: