I've tried different things to create the swift equivalent of this cURL request, but I couldn't get it working.
curl -X POST -F "file=@/Users/nicolas/sample.png" -F "mode=document_photo" https://api.idolondemand.com/1/api/sync/ocrdocument/v1 -F "apikey=xxx-xxx-xxx-xxx-xxx"
The respective code is posted below.
func getText (image: UIImage){
let apiKey = "xxx-xxx-xxx-xxx-xxx"
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.idolondemand.com/1/api/sync/ocrdocument/v1")!)
request.HTTPMethod = "POST"
request.addValue(apiKey, forHTTPHeaderField: "apikey")
request.addValue("document_photo", forHTTPHeaderField: "mode")
request.HTTPBody = UIImageJPEGRepresentation(image, 1)
let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: UIImageJPEGRepresentation(image, 1), completionHandler: {data, response, error -> Void in
if let _ = data {
var error:NSError? = nil
do {
let jsonObject : AnyObject = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
let json = JSON(jsonObject)
if let ocr_results = json["text_block"][0]["text"].string {
self.returnText(ocr_results)
}
} catch let error1 as NSError {
error = error1
print(error)
} catch {
fatalError()
}
}
})
I would be happy if I get a response.
In the curl command, you are defining the form fields file
, mode
and apikey
, and you need to encode them in the form multipart/form-data
and put this in the HTTPBody
. Some quick googling reveals the library SRWebClient (among many others) that will help you make this sort of request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With