如何解决发布请求中的错误请求(代码400)错误?

时间:2019-04-23 01:11:41

标签: swift rest api alamofire http-status-code-400

我正在我的应用程序中添加一个Agreement Module,当用户同意时,用户会在其中点击复选框。当用户点击复选框时,它将返回一个true值并启用proceed按钮。点击proceed按钮后,它将激活用户的帐户。问题是,它返回一个status code 400并且没有激活用户的帐户。我无法弄清楚我的代码的哪一部分有错误,或者错误来自API端。

我尝试使用与PostMan文件中使用的相同参数在APIService.swift中对其进行检查,它返回status code 200并激活用户帐户。

APIService.swift

func createUser(userRole: UserRole, credential: String, email: String, secureText: String, termsConditions: Bool, completion: @escaping validateTaskCompletion) {

guard let createUserURL = URL(string: CREATE_USER_URL) else {
    completion(nil, .InvalidURL)
    return
}

let firstParameterTitle = "firstCredential"
let secondParameterTitle = "userType"
let thirdParameterTitle = "email"
let fourthParameterTitle = "password"
let fifthParameterTitle = "hasAgreedToTermsConditions"

let requestBody: Parameters = [

    firstParameterTitle: credential,
    secondParameterTitle: userRole.rawValue,
    thirdParameterTitle: email,
    fourthParameterTitle: secureText,
    fifthParameterTitle: termsConditions
]

Alamofire.request(createUserURL, method: .post, parameters: requestBody).responseJSON(completionHandler: { (response) in

    guard reachability(responseResult: response.result) else {
        completion(nil, .NoNetwork)
        return
    }

    guard let statusCode = response.response?.statusCode else {
        completion(nil, .NoStatusCode)
        return
    }

    switch(statusCode) {
    case 200:
        completion(true, nil)
    case 400:
        completion(nil, .BadRequest)
    case 404:
        completion(nil, .InvalidCredentials)
    case 409:
        completion(nil, .AlreadyActivated)
    default:
        completion(nil, .UncapturedStatusCode)
        print("**createUser** Uncaptured Status Code: \(statusCode)")
    }

})

TermsAndConditionVC.swift

var userRole: UserRole!
var firstCredential: String!
var emailString: String!
var confirmPasswordString: String!
var agreedtoTermsConditions: Bool!

@IBAction func checkedButtonTapped(_ sender: UIButton) {

    if sender.isSelected {
        agreedtoTermsConditions = false
        sender.isSelected = false
        self.proceedButton.isEnabled = false

    } else {
        agreedtoTermsConditions = true
        sender.isSelected = true
       self.proceedButton.isEnabled = true

}
}

 @IBAction func proceedButtonTapped(_ sender: UIButton) {

    createUser(userRole: userRole!, credential: firstCredential!, email: emailString, secureText: confirmPasswordString, termsConditions: agreedtoTermsConditions) { (success, error) in

        guard success == true, error == nil else {

            if let networkError = error {

                if networkError == .AlreadyActivated {
                    _ = SCLAlertView(appearance: appearance).showError("Already Activated", subTitle: "\(networkError.rawValue)")
                }else if networkError == .BadRequest {
                    _ = SCLAlertView(appearance: appearance).showError("Error", subTitle: "There is something went wrong. Please try again.")
                }else {
                    _ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "\(networkError.rawValue)")
                }
            }

            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "hideLoading"), object: nil)
            return
        }

    }

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "nextStep"), object: nil)

}

0 个答案:

没有答案