Want to Drive Innovation through Crowdsourcing? Learn the 5 Steps Get the eBook ×

HP IDOL OnDemand APIs are “Swift” and easy!

By wwwtc In Community Stories, Help Articles

Posted September 12th, 2014

Next week we are kicking off a new 30 day series for HP IDOL OnDemand to build #awesome stuff with Swift. There’s $20,000 in prize money up for grabs so I wanted to put together a quick blog post on how stupid-simple it is to use the HP IDOL OnDemand APIs.

So here’s a quick Playground that uses the OCR Document API to extract the text from the following image. You can Try the API for yourself in the HP IDOL OnDemand API playground with the image URL to test the request.


You can also cURL the request and it should look something like:

Since parsing JSON is not as simple as it should be, I’m using SwiftyJSON to make my life easier. I used the Synchronous version of the API since the image is small and the API is lightning fast!

The first thing we need to do is import the XCPlayground framework and add XCPSetExecutionShouldContinueIndefinitely(). This allows for asynchronous operations so that Playground can return the results from the API call after all top levels commands have executed and the process normally ends.

Then, of course, we need to paste the contents of the SwiftyJSON source into our Playground so we get all the JSON-parsing goodness.

The next few lines of code setup our POST request to the API to OCR the image and return the resulting text. We create the task on line 19 and then kick it off on line 34 with task.resume(). The interesting part of the task is the last parameter of uploadTaskWithRequest which is a closure that gets called upon completion of the request. This function parses the JSON results and println the resulting text.

Your Playground should display the following if all goes well:

How do you get started with HP IDOL OnDemand? Simple. Check out the community site to get your free API Key. There’s links to code snippets, documentation and an API playground where you can run sandbox requests with your own or sample data.