使用Alamofire的cURL-Swift-multipart/form-data [英] cURL with Alamofire - Swift - multipart/form-data

查看:223
本文介绍了使用Alamofire的cURL-Swift-multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,如果这个问题很愚蠢,但是对这件事我还是很陌生.我已经尝试了其他方法来使用Alamofire创建与该cURL请求一样的快捷方式,但是我不知道如何将图像作为多部分/表单数据发送到API.

First, im sorry if this question is stupid but im pretty new to this stuff. I've tried different things to create the swift equivalent of this cURL request with Alamofire, but I don't know how to send the image as a multipart/form-data to the 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"

对于这种类型的请求,我认为当前代码是非常错误的,但是我还是要为您发布:

I think current code is pretty wrong for this type of request, but still i'm gonna post it for you:

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,但是由于我尝试将图像发送到用户使用相机拍摄的服务器上,所以我只能发送图像文件.

The only way how it worked for me until now, was using a URL, but since I try to send a image to the server that the user took with the camera, i only can send a image file.

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

如果得到回应,我会很高兴,因为这使我发疯.

I would be happy if I get a response, because this is driving me crazy.

ps.我正在使用Swift 2.0

ps. Im using swift 2.0

推荐答案

Alamofire在其

Alamofire has an example in their documentation using Alamofire.upload(_:URLString:headers:multipartFormData:encodingMemoryThreshold:encodingCompletion:) that looks like it will answer your question (note that in their example, the headers and encodingMemoryThreshold parameters have a default value if you don't supply one).

另请参阅 MultipartFormData 类上有关各种appendBodyPart()方法的文档.实例.

Also see their documentation on the various appendBodyPart() methods on a MultipartFormData class instance.

因此,可以修改您提供的示例代码的一种方式:

So, a way in which your provided example code could be modified might be:

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

这篇关于使用Alamofire的cURL-Swift-multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆