Alamofire: Running Automatic validation on 'upload' call?

Created on 16 Feb 2016  路  5Comments  路  Source: Alamofire/Alamofire

I am trying to validate the below call,

Alamofire.upload(
    .POST,
    "https://httpbin.org/post",
    multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
        multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    }
)

It always lands up in .Success case, even when I have a 404/500 error.
How do I auto validate this call? Is there a way to call validate on upload calls?

support

Most helpful comment

Below is a snippet taken from my recent project, you can refer this for the usage of validate method.

import Alamofire
import SwiftyJSON
...

let parameters = ["somekey": "somevalue"]
Alamofire.upload(
    .POST,
    Config.url("/url/to/profile/update"),
    headers: ["Session-Token": "mytokenstring"],
    multipartFormData: { multipartFormData in
        if let _image = self.profile.image {
            let orientationFixedImg = _image.fixOrientation()
            if let imageData = UIImageJPEGRepresentation(orientationFixedImg, 0.7) {
                multipartFormData.appendBodyPart(data: imageData, name: "photo", fileName: "profile.jpg", mimeType: "image/jpg")
            }
        }
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.validate(statusCode: 200...299)
                .validate(contentType: ["application/json"])
                .response { response in
                    upload.responseJSON {res in
                        if(!res.result.isSuccess) {
                            //Something went wrong
                            return
                        } else {
                            let response = res.result.value as! NSDictionary
                            //success
                            //var jsonData = JSON(response)
                            //print(jsonData)
                        }
                    }
            }
        case .Failure(let encodingError):
            print(encodingError)
            //let jsonData = JSON(data: response.data!)
        }
    }
)

All 5 comments

You need to add upload.validate() before upload.responseJSON in the .Success case. In the future, these types of questions are better suited for Stack Overflow. We use GitHub for bug reports and feature requests.

Cheers. 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

Usage wasn't clear from the docs.

I did ask this on SO, no one responded, therefore I thought this could be an issue with the Alamofire.upload() method hence I opened an issue here.

Thanks!

@cnoon Hi! I met relative problems of this issue.
when I add upload.validate(), how could I handle the invalidated case (such as status code 400). Bacause the validate is in the .success case, response with status 400 will be ignored instead of return .failure error.
Thanks!

Below is a snippet taken from my recent project, you can refer this for the usage of validate method.

import Alamofire
import SwiftyJSON
...

let parameters = ["somekey": "somevalue"]
Alamofire.upload(
    .POST,
    Config.url("/url/to/profile/update"),
    headers: ["Session-Token": "mytokenstring"],
    multipartFormData: { multipartFormData in
        if let _image = self.profile.image {
            let orientationFixedImg = _image.fixOrientation()
            if let imageData = UIImageJPEGRepresentation(orientationFixedImg, 0.7) {
                multipartFormData.appendBodyPart(data: imageData, name: "photo", fileName: "profile.jpg", mimeType: "image/jpg")
            }
        }
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.validate(statusCode: 200...299)
                .validate(contentType: ["application/json"])
                .response { response in
                    upload.responseJSON {res in
                        if(!res.result.isSuccess) {
                            //Something went wrong
                            return
                        } else {
                            let response = res.result.value as! NSDictionary
                            //success
                            //var jsonData = JSON(response)
                            //print(jsonData)
                        }
                    }
            }
        case .Failure(let encodingError):
            print(encodingError)
            //let jsonData = JSON(data: response.data!)
        }
    }
)

@r-a-o Thanks for your example!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

solomon23 picture solomon23  路  3Comments

yamifr07 picture yamifr07  路  3Comments

shivang2902 picture shivang2902  路  3Comments

filippovdev picture filippovdev  路  3Comments

DrAma999 picture DrAma999  路  3Comments