Reputation: 2237
I am using Alamofire to try and put an image on image shack server using the image shack API. I am not getting a response back and get the error:
FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0.
I have highlighted where the error is referring too in the code also:
let imageName = "pano.jpg"
let postImage = UIImage(named: imageName)
let urlStr = "https://post.imageshack.us/upload_api.php"
let theUrl:NSString = urlStr.stringByRemovingPercentEncoding!
let url = NSURL(string: theUrl as String)!
let imgData = UIImageJPEGRepresentation(postImage!, 0.2)!
let keyData = "0345CHKMad115dd32980b363be5f2d34731b8110".dataUsingEncoding(NSUTF8StringEncoding)!
let keyJSON = "json".dataUsingEncoding(NSUTF8StringEncoding)!
Alamofire.upload(.POST, url, multipartFormData: { MultipartFormData in
MultipartFormData.appendBodyPart(data: imgData, name: "fileupload", mimeType: "image/jpg")
MultipartFormData.appendBodyPart(data: keyData, name: "key")
MultipartFormData.appendBodyPart(data: keyJSON, name: "format")
}) {encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON(completionHandler: { response in
print(response) //ERROR HERE
guard let data = response.result.value
else{
print("Request failed with error data)")
return
}
if let info = data as? Dictionary<String, AnyObject> {
if let links = info["links"] as? Dictionary<String, AnyObject> {
if let imgLink = links["image_link"] as? String {
print(imgLink)
}
}
}
})
I am completely lost what this problem means any help would be great.
Upvotes: 0
Views: 855
Reputation: 11
switch encodingResult {
case .Success(let upload, _,_):
upload.responseJSON(completionHandler: {response in
if let data = response.result.value as? Dictionary<String, AnyObject>{
if let links = data["links"] as? Dictionary<String, AnyObject> {
if let imgLink = links["image_link"] as? String {
print(imgLink)
}
}
}
})
case .Failure(let error):
print(error)
}
Upvotes: 0
Reputation: 8279
Try this, before passing your url
into the function do:
//Convert the NSURL to a NSString
let urlString:NSString = url.absoluteString() as! NSString
//Remove Percent Encoding
let theUrl:NSString = urlString.stringByRemovingPercentEncoding()
//Place Back into NSURL
let newUrl = NSURL(string:theUrl)
And pass newUrl
instead of url
. It would be better to do this before instantiation of the original url
, but try that and see if that helps.
EDIT
try this too where you are having the issue:
var jsonError:NSError?
let json = NSJSONSerialization.JSONObjectWithData(response, options: nil, error: &jsonError) as NSDictionary {
print(json)
}
Upvotes: 0