Im trying to get a JSON file from an API, but Alamofire (branch: Swift 2.0) returns the file with a strange prefix (Alamofire.Result.Success). I'm sorry if this is a stupid question, but im new to alamofire. How can I just get a NORMAL file that I can use with SwiftyJSON.
My code:
func getText (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,
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: "image.png", mimeType: "image/png")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { _, _, json in
print(JSON)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
}
Output from print(JSON):
Alamofire.Result<Swift.AnyObject>.Success([text_block: (
{
height = 127;
left = 0;
text = "\U2022 response()\n\U2022 responseString(encoding: NSStringEncoding)\n\U2022 responseJSON(options: NSJS0NReading0ptions)\n\U2022 responsePropertyList(options: NSPropertyListRead0ptions)";
top = 0;
width = 487;
}
)])
In the closure argument for your call to upload.responseJSON(_:completionHandler:)
, the parameter you've given the name json
is being passed as an Alamofire Result
Enumeration (see source).
You should be able to get at the associated data for that enum by accessing its value
property, like so:
upload.responseJSON { _, _, json in
print(json.value!)
}
Additionally, check out SwiftyJSON when you get a chance (a popular library for handling JSON in Swift).