yun93j yun93j - 7 months ago 511
Swift Question

Alamofire .responseJSON doesn't work with JSON response

I'm using alamofire to upload an image.
This is sign up API where you can sign up with your profile photo (this is what I have to upload)

So my code is this (I will replace print(JSON) with another code; this is just for testing what's wrong)

func makeUploadRequest(url: String?) {

let imageData = NSData(data: UIImageJPEGRepresentation(self.userImage.image!, 1)!)

Alamofire.upload(.POST, url!, headers: ["Content-Type":"application/json"], multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "image_file_1")
}, encodingCompletion: {
encodingResult in

switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { (JSON) in
print(JSON)
}

case .Failure(let encodingError):
//Show Alert in UI
print(encodingError)
}
})
}


but when I run this code, I come across with this message:

FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around
character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}


I know why I get this message, it's because the response is not in JSON format.
But the response is actually JSON

{
result: "success",
msg: "",
data: {...}
}


When I test the API with URL, it works just fine.

When I used .responseString instead of .responseJSON: it said something about ASP.NET

.response:

(Optional(<NSMutableURLRequest: 0x7f8353217d50> { URL: url }),
Optional(<NSHTTPURLResponse: 0x7f8353099040> { URL: url }
{ status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 5136;
"Content-Type" = "text/html; charset=utf-8";
Date = "Tue, 26 Apr 2016 06:09:03 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "2.0.50727";
"X-Powered-By" = "ASP.NET";
} }), Optional(<3c68746d ... 2e2d2d3e>), nil)


Any help? Thanks in advance!

Answer

Try to replace Success block :-

case .Success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                    AppHelper.hideLoadingSpinner()
                    if response.result.value is NSNull
                    {
                        print("Response nil")
                    }
                    else
                    {
                        if let JSON = response.result.value {
                            var mydict = NSMutableDictionary()
                            mydict = JSON as! NSMutableDictionary

                            if (self.delegate != nil){
                                print("response:--------------------\n %@",mydict)
                            }
                        }
                        else
                        {
                            print("response not converted to JSON")
                        }

                    }
                    upload.response(completionHandler: { (request, response, data, error) -> Void in

                            NSLog("upload.response : data : %@", String(data: data!, encoding: NSUTF8StringEncoding)!)
                            NSLog("upload.response : response : %@", response!)


                        })
    }
Comments