- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
首先,如果这个问题很愚蠢,我很抱歉,但我对这个东西还很陌生。我尝试了不同的方法来创建与 Alamofire 的 cURL 请求的快速等价物,但我不知道如何将图像作为 multipart/form-data 发送到 API。
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 getOCR(image: UIImage) {
let url = "https://api.idolondemand.com/1/api/sync/ocrdocument/v1"
let apiKey = "xxx-xxx-xxx-xxx-xxx"
let imageData = UIImagePNGRepresentation(image)
Alamofire.request(.POST, url, parameters: ["apikey": apiKey, "file": imageData!]).responseJSON() {
_,_,JSON in
print(JSON)
}
}
到目前为止,它对我唯一有效的方法是使用 URL,但由于我尝试将用户用相机拍摄的图像发送到服务器,所以我只能发送图像文件。
网址代码:
func test(url: NSURL) {
let url = "https://api.idolondemand.com/1/api/sync/ocrdocument/v1"
let apiKey = "xxx-xxx-xxx-xxx-xxx"
Alamofire.request(.POST, url, parameters: ["apikey": apiKey, "url": url]).responseJSON() {
_,JSON,_ in
print(JSON)
}
}
如果我得到回应我会很高兴,因为这让我发疯。
附言。我正在使用 swift 2.0
最佳答案
Alamofire 在他们的 documentation 中有一个例子使用 Alamofire.upload(_:URLString:headers:multipartFormData:encodingMemoryThreshold:encodingCompletion:)
看起来会回答您的问题(请注意,在他们的示例中,headers
和encodingMemoryThreshold
如果您不提供参数,则参数具有默认值)。
另请参阅关于 MultipartFormData 上各种 appendBodyPart()
方法的文档类实例。
因此,可以修改您提供的示例代码的方法可能是:
func getOCR(image: UIImage) {
let url = "https://api.idolondemand.com/1/api/sync/ocrdocument/v1"
let apiKey = "xxx-xxx-xxx-xxx-xxx"
let mode = "document_photo"
let imageData = UIImagePNGRepresentation(image)
Alamofire.upload(
.POST,
URLString: url,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(
data: apiKey.dataUsingEncoding(NSUTF8StringEncoding)!,
name: "apikey"
)
multipartFormData.appendBodyPart(
data: mode.dataUsingEncoding(NSUTF8StringEncoding)!,
name: "mode"
)
multipartFormData.appendBodyPart(
data: imageData!,
name: "file",
fileName: "testIMG.png",
mimeType: "image/png"
)
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { _, _, JSON in println(JSON) }
case .Failure(let encodingError):
println(encodingError)
}
}
)
}
关于swift - cURL 与 Alamofire - Swift - 多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31834704/
我一直在开发一个 .NET 字符串格式化库来协助应用程序的本地化。它称为 SmartFormat 并在 GitHub 上开源. 它试图解决的问题之一是 Grammatical Numbers .这也称
leetcode关于单数II的题目是: 给定一个整数数组,除一个元素外,每个元素出现三次。找到那一个。笔记:您的算法应该具有线性运行时复杂度。你能在不使用额外内存的情况下实现它吗? 其实我已经从网站上
我想知道创建/命名模型的首选方法是什么? 我的应用程序有一个“用户”模型,其中包含用于创建、获取、更新(等)用户记录的所有相关业务逻辑。 在我的一些 Controller 中,我可能想要获取多个“用户
在我的 Mysql 数据库中,我有一个术语列表,例如(首字母大写,大多数时候是复数) Hairdressers Restaurants Beauty Salons Fournitures For Re
如果我决定为我的所有路线名称使用复数形式,但某些资源仅作为一个东西存在,您是否将其保持为单数(更直观)或尊重使用复数的决定并保持这种方式? 我们正在用 PHP 为我们的客户门户网站设计一个新的 API
我可能在做一些愚蠢的事情,但是...... 应用/模型/user.rb: class User 然后,当我导航到 /users/123/totem/new 时,出现错误: ActionView::
您能否澄清一些 Matplotlib 术语: “subplots”(或“subplot”?)这个词是“axes”的同义词吗? “轴”和“轴”的单数/复数是什么? 最佳答案 这确实是一个令人困惑的问题。
我有一个 profile我的应用程序中的模型。我想允许用户通过 /profile 查看他们自己的个人资料,所以我创建了这条路线: resource :profile, :only => :show 我
我是一名优秀的程序员,十分优秀!