xcode 无法从 Alamofire 3.3.0 获取服务器错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36331234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:41:13  来源:igfitidea点击:

Could not get the server error message from Alamofire 3.3.0

iosxcodeswiftalamofire

提问by Joseph Wahba

It is my first time for me to use Alamofire, and it got me really frustrated.

这是我第一次使用Alamofire,这让我非常沮丧。

I'm using the following code to call a signup API on the backend API

我正在使用以下代码在后端 API 上调用注册 API

Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON)
            .validate(statusCode: 200..<300)
            .validate(contentType: ["application/json"])
            .responseJSON { response in
                switch response.result {
                case .Success(let JSON):
                    print("Success with JSON: \(JSON)")
                    success(updatedUser)
                case .Failure(let error):
                    print("Request failed with error: \(error)")
                    failure(error)
                }

        }

The problem is that the error object I'm getting in the .Failurefunction doesn't contain the server side message. I have tried to access the rest of the objects (request, response, data, result) I could not find my error message anywhere

问题是我在.Failure函数中得到的错误对象不包含服务器端消息。我试图访问其余的对象(请求、响应、数据、结果)我在任何地方都找不到我的错误消息

I'm always getting the following error, no matter what the server message has to say. Request failed with error:

无论服务器消息怎么说,我总是收到以下错误。请求失败,错误:

FAILURE: Error Domain=com.alamofire.error Code=-6003 "Response status code was unacceptable: 400" UserInfo={NSLocalizedFailureReason=Response status code was unacceptable: 400}

失败:错误域=com.alamofire.error 代码=-6003“响应状态代码不可接受:400” UserInfo={NSLocalizedFailureReason=响应状态代码不可接受:400}

Is there is anything wrong I'm doing?

我做错了什么吗?

Swift 2.2, AlamoFire 3.3.0, Xcode 7.3

Swift 2.2、AlamoFire 3.3.0、Xcode 7.3

回答by Joseph Wahba

I managed to get it to work exactly the way I want is by dropping the status validation and check for the statusCode manually

我设法让它完全按照我想要的方式工作,方法是删除状态验证并手动检查 statusCode

Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON)
            .validate(contentType: ["application/json"])
            .responseJSON { response in
                if response.response?.statusCode == 200 {
                    print("Success with JSON: \(response.result.value)")

                    success(updatedUser)
                }
                else {
                    let error = response.result.value as! NSDictionary
                    let errorMessage = error.objectForKey("message") as! String
                    print(errorMessage)
                    failure(errorMessage)
                }


        }

回答by Kiran jadhav

updated for swift 3 :

为 swift 3 更新:

Used below lines of code:-

使用以下代码行:-

    Alamofire.request(escapedString!, method: .get, encoding: JSONEncoding.default)
        .validate(contentType: ["application/json"])
        .responseJSON { response in
            if response.response?.statusCode == 200 {
                print("Success with JSON: \(String(describing: response.result.value))")

            }
            else {
                let error = (response.result.value  as? [[String : AnyObject]])
                print(error as Any)
            }
    } 

回答by SafeFastExpressive

This is how to get the error domain, code and user info with Alamofire 4 and Swift 3. User info contains the error strings.

这是使用 Alamofire 4 和 Swift 3 获取错误域、代码和用户信息的方法。用户信息包含错误字符串。

Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON)
            .validate(statusCode: 200..<300)
            .validate(contentType: ["application/json"])
            .responseJSON { response in
                switch response.result {
                case .Success(let JSON):
                    print("Success with JSON: \(JSON)")
                    success(updatedUser)
                case .Failure(let error):
                    let errorCode = error._code
                    let errorDomain = error._domain
                    let userInfo = error._userInfo
                    print("Request failed with error: \(error), code: \(errorCode), domain: \(errorDomain)")
                    failure(error)
                }

        }