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
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, theheaders
andencodingMemoryThreshold
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: