Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Swift中的cURL等价 – iOS大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我已经尝试了不同的东西来创建这个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(以及许多其他)将帮助您提出这种请求.

大佬总结

以上是大佬教程为你收集整理的在Swift中的cURL等价 – iOS全部内容,希望文章能够帮你解决在Swift中的cURL等价 – iOS所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: