cURL with Alamofire - Swift - multipart/form-data

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 02:25:48

问题


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

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 Code:

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. Im using swift 2.0


回答1:


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

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


来源:https://stackoverflow.com/questions/31834704/curl-with-alamofire-swift-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!