加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中的cURL等价 – iOS

发布时间:2020-12-14 04:55:39 所属栏目:百科 来源:网络整理
导读:我已经尝试了不同的东西来创建这个cURL请求的快速等价物,但我无法让它工作. 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" 相应的
我已经尝试了不同的东西来创建这个cURL请求的快速等价物,但我无法让它工作.

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"

相应的代码发布在下面.

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()
            }
        }

})

如果我收到回复,我会很高兴.

解决方法

在curl命令中,您正在定义表单字段文件,模式和apikey,您需要以multipart / form-data格式对它们进行编码,并将其放在HTTPBody中.一些快速的谷歌搜索显示库 SRWebClient(以及许多其他)将帮助您提出这种请求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读