1

Getting Response From the Server Even if the passing the parameters and the header file

I also tried the encoding: JSONEncoding.default and JSONEncoding.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)")
            }
        }

    }
Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
Vishal Shelake
  • 572
  • 1
  • 8
  • 12
  • You can do this (https://stackoverflow.com/questions/53637437/alamofire-with-d/53637821#53637821) what the request really looks like. And check that since you tell the params are `x-www-form-urlencoded` that's really the case for instance (or missing something else). – Larme Jan 20 '19 at 11:36

2 Answers2

1
  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)")
        }
    }

**This is The Code Will Work **

Vishal Shelake
  • 572
  • 1
  • 8
  • 12
0

The error returned is not about a missing parameter, it's about an Authorization error, I imagine your server is needs OAuth or other authorization before the actual request.

Mosbah
  • 1,347
  • 1
  • 14
  • 28
  • The same API is already working for Android we are using OAuth Authentication but getting an error the same as listed above – Vishal Shelake Jan 20 '19 at 08:10