Alamofire POST请求,正文中有一个简单字符串作为参数,标题仍然显示缺少的参数值

时间:2019-01-20 06:33:53

标签: ios swift alamofire

从服务器获取响应,即使传递parameters和头文件

我还尝试了encoding: JSONEncoding.defaultJSONEncoding.JSON

let URL_GET_DATA = http://abcdef.com:4207/base/signin

let headers = [
            "Authorization": "Bearer 5baa9fd27862adc397033f1ff27d73b0c2318144",
            "Content-Type":  "application/x-www-form-urlencoded"
        ]

         let parameters: Parameters = [
            "Start": "1",
            "end" : "11"
        ]
Alamofire.request(URL_GET_DATA, method: .post, parameters: parameters as! Parameters, headers: headers).responseJSON { response in
            print("Request  \(response.request)")

            print("RESPONSE \(response.result.value)")
            print("RESPONSE \(response.result)")
            print("RESPONSE \(response)")


            switch response.result {
            case .success:
                print(response)
                let dictVal = response.result.value
                let dictMain:NSDictionary = dictVal as! NSDictionary
                let statusCode = dictMain.value(forKey: "statusCode") as! Int
                let statusMsg = dictMain.value(forKey: "statusMsg") as! String
                print(statusMsg)
                print(statusCode)
                                //if the auth is sucessfull then
                if(statusCode == 0)
                {
                    let dictVal1 = dictMain.value(forKey: "data") as! NSDictionary
                    //get the responce code or Token From the server
                    let statusCode1 = dictVal1.value(forKey: "accessToken") as! String
                    let statusMsg1 = dictVal1.value(forKey: "refreshTokenExpiresAt") as! String

                    //Store data Locally to create session
                    UserDefaults.standard.set(false, forKey: "userSession")
                    print(statusCode1)
                    print(statusMsg1)

                    //dissmiss the screen and go to the home page
                    //self.dismiss(animated: true, completion: nil)


                }
                    // if the auth is Failre
                else if(statusCode == 1)
                {
                    self.displayMyAlertMessage(userMessage: "Please Enter The Correct Credencial, Username or pasword is incorrect ")
                }
                case .failure(let error):
                print(error)
                self.displayMyAlertMessage(userMessage: "You are not Connected to the Internet Please Check The connection. \(error)")
            }
        }

    }

2 个答案:

答案 0 :(得分:0)

返回的错误与丢失参数无关,而是与授权错误有关,我想您的服务器在实际请求之前需要OAuth或其他授权。

答案 1 :(得分:0)

  let URL_GET_DATA = "http://abcdef.com:4207/base/signin"


    let parameters = [
        "email_id": userEmail, //email
        "password": userPassword //password
    ]
    var statusCode: Int = 0
    print(parameters)
    //Almofire Code
    Alamofire.request(URL_GET_DATA, method: .post, parameters: parameters).responseJSON { response in
        print(response)

        switch response.result {
        case .success:
            print(response)
            let dictVal = response.result.value
            let dictMain:NSDictionary = dictVal as! NSDictionary
            let statusCode = dictMain.value(forKey: "statusCode") as! Int
            let statusMsg = dictMain.value(forKey: "statusMsg") as! String
            print(statusMsg)
            print(statusCode)

            //if the auth is sucessfull then
            if(statusCode == 0)
            {
                let dictVal1 = dictMain.value(forKey: "data") as! NSDictionary
                //get the responce code or Token From the server
                var statusCode1="Bearer "
                statusCode1 += dictVal1.value(forKey: "accessToken") as! String
                //                    let statusCode1 = dictVal1.value(forKey: "accessToken") as! String
                let statusMsg1 = dictVal1.value(forKey: "refreshTokenExpiresAt") as! String

                //Store data Locally to create session
                UserDefaults.standard.set(false, forKey: "userSession")
                UserDefaults.standard.set(statusCode1, forKey: "accessToken")

                //to pass the email to the Header file
                UserDefaults.standard.set(userEmail, forKey: "emailSession")
                let emailId = UserDefaults.standard.object(forKey: "emailSession")
                print(emailId!)
                //                    print(statusCode1)
                //                    print(statusMsg1)

                //dissmiss the screen and go to the home page
                self.dismiss(animated: true, completion: nil)


            }

                // if the auth is Failre
            else if(statusCode == 1)
            {
                self.displayMyAlertMessage(userMessage: "Please Enter The Correct Credencial, Username or pasword is incorrect ")

            }
        case .failure(let error):
            print(error)
            self.displayMyAlertMessage(userMessage: "You are not Connected to the Internet Please Check The connection. \(error)")
        }
    }

**这是该代码将起作用的**